4

I am writing a smal REST API in ExpressJS using Mongoose. I have written unit tests for all of the logic inside the app. The only thing that is left untested now is if the API returns the proper status codes and if the restricted resources are in fact restricted.

What I have tried so far:

I tried to use Request to perform GET/POST/PUT/DELETE on a resource, but I find it very clumsy to throw stuff into the db, keep references to the objects and delete it again later.

Is this the smartest way of approaching this? Is my implementation wrong if I need to test something through actual requests with an actual database?

If this is the prefered way of testing the API, is it necessary to test the internal functionality as a complete solution, or should I just test the status codes and the access.

1 Answer 1

4

We usually create a test DB and run the tests with NODE_ENV='test' ... which triggers a connection to the test DB.

That way you can wipe the test DB no problem and you don't have to worry about cleaning up before/after tests.

A useful tool for REST testing is superagent (or supertest, though I prefer agent):

Full disclosure: I'm a contributor to superagent.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I have tried that, but if that's whats most common I'll give it another try. I'll have a look at the libraries, they look better than request. So for what to test, just test the status codes and what I have access to and ignore what content I get as a response? Anything else I should test?
I usually test every route in a manner like: 1) golden path, 2) bad input, 3) insufficient access
Thanks again. This got me on the right tracks, and superagent is awesome!
Superagent is the best.
I am using supertest to test my rest api. But when I use test DB, how can I generate the test data for my testing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.