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.

kimpettersen
  • 1,602
  • 2
  • 13
  • 18

1 Answers1

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.

hunterloftis
  • 13,386
  • 5
  • 48
  • 50
  • 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? – kimpettersen Feb 03 '13 at 00:08
  • I usually test every route in a manner like: 1) golden path, 2) bad input, 3) insufficient access – hunterloftis Feb 03 '13 at 20:36
  • Thanks again. This got me on the right tracks, and superagent is awesome! – kimpettersen Feb 04 '13 at 00:19
  • Superagent is the best. – demisx Mar 11 '15 at 20:23
  • 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? – Dzanvu Apr 28 '15 at 07:43