RejectedSoftware Forums

Sign up

Unit testing web framework

Hey guys,

I'm curious about how to approach unit testing a class being utilized in the web framework, e.g checking routes return the correct content, that auth is being check correctly, etc.

I have seen the createTestHTTPServerRequest and createTestHTTPServerResponse functions, but I'm not entirely sure how to utilize them, does anyone have examples?

Thanks

Re: Unit testing web framework

On Tue, 27 Dec 2016 07:53:36 GMT, Joshua Hodkinson wrote:

Hey guys,

I'm curious about how to approach unit testing a class being utilized in the web framework, e.g checking routes return the correct content, that auth is being check correctly, etc.

I have seen the createTestHTTPServerRequest and createTestHTTPServerResponse functions, but I'm not entirely sure how to utilize them, does anyone have examples?

Thanks

There are some simple uses in the router unit tests. If you need to send a body, you can pass a MemoryStream to createTestHTTPServerRequest (or any other stream type).

However, this API alone is not particularly convenient when doing more complex tests. There is an open pull request that attempts to implement a nicer API on top. It got stuck in a state of discussion, but should eventually become part of the library. For now, you could manually take the code there and use it for implementing the tests.

Re: Unit testing web framework

On Tue, 27 Dec 2016 21:07:05 GMT, Sönke Ludwig wrote:

On Tue, 27 Dec 2016 07:53:36 GMT, Joshua Hodkinson wrote:

Hey guys,

I'm curious about how to approach unit testing a class being utilized in the web framework, e.g checking routes return the correct content, that auth is being check correctly, etc.

I have seen the createTestHTTPServerRequest and createTestHTTPServerResponse functions, but I'm not entirely sure how to utilize them, does anyone have examples?

Thanks

There are some simple uses in the router unit tests. If you need to send a body, you can pass a MemoryStream to createTestHTTPServerRequest (or any other stream type).

However, this API alone is not particularly convenient when doing more complex tests. There is an open pull request that attempts to implement a nicer API on top. It got stuck in a state of discussion, but should eventually become part of the library. For now, you could manually take the code there and use it for implementing the tests.

Ok thanks for that, how might I go about mocking up a mongo connection? I could start a new instance with the pre build commands in the test configuration but that's starting to approach integration tests.

I might need to consider having interface which would make testing easier + moving to a rest api at a later date easier.

Re: Unit testing web framework

Am 28.12.2016 um 11:18 schrieb Joshua Hodkinson:

On Tue, 27 Dec 2016 21:07:05 GMT, Sönke Ludwig wrote:

On Tue, 27 Dec 2016 07:53:36 GMT, Joshua Hodkinson wrote:

Hey guys,

I'm curious about how to approach unit testing a class being utilized in the web framework, e.g checking routes return the correct content, that auth is being check correctly, etc.

I have seen the createTestHTTPServerRequest and createTestHTTPServerResponse functions, but I'm not entirely sure how to utilize them, does anyone have examples?

Thanks

There are some simple uses in the router unit tests. If you need to send a body, you can pass a MemoryStream to createTestHTTPServerRequest (or any other stream type).

However, this API alone is not particularly convenient when doing more complex tests. There is an open pull request that attempts to implement a nicer API on top. It got stuck in a state of discussion, but should eventually become part of the library. For now, you could manually take the code there and use it for implementing the tests.

Ok thanks for that, how might I go about mocking up a mongo connection? I could start a new instance with the pre build commands in the test configuration but that's starting to approach integration tests.

I might need to consider having interface which would make testing easier + moving to a rest api at a later date easier.

Yeah, for MongoDB there is unfortunately no direct way to replace it
with a mock. Wrapping it in a higher level interface/class is what I
usually do, but passing the MongoConnection/MongoCollection as a
template type and making an impostor struct would be another possibility
(basically duck typing).

Once I started to write a pretty generic database abstraction
(MongoDB, Cassandra, MySQL or memory/file table based). If that would
fully work now, I'd probably use that directly as the main DB
abstraction. Creating an in-memory database with pre-defined data for
testing would be very straight forward then. Maybe I can get back to
that one day (or someone comes up with a similar implementation first).