Strategy for testing POST to API without changing database
Asked Answered
G

4

8

I'm using jasmine-node to test my API, and it has worked great for my GET routes. Now, however, I need to test some POSTs and I'm not sure how to go about this without changing my database.

One thought I had was to reset whatever value I change at the end of each spec.

Is this reasonable or is there a better way to go about testing POST requests to my API?

Gwendolin answered 22/7, 2015 at 16:10 Comment(0)
I
5

Wrap anything that modifies your database into a transaction. You can have your database changes and then rollback after each test.

Ideatum answered 22/7, 2015 at 16:20 Comment(0)
W
2

usually you are supposed to have a test database, so modify that one is not a big issue. also, a general approach would be not to rely on predefined values on the database (i.e, the GET always request the SAME object..) but try with different objects each time. (using predefined objects may hide problems when the data is slighty different..). in order to implement the second strategy, you can execute a test with a POST with pseudo-random data to create a new object, and use the returned ID to feed the following GET, UPDATE and finally the DELETE tests.

Whiteside answered 22/7, 2015 at 16:22 Comment(0)
P
1

Just make a duplicate processing page/function and send the data to that for debugging. Comment out anything that makes changes to the database.

Alternatively, pass a variable in your call such as "debug" and have an if/else section in your original function for debugging, ignoring the rest of the function.

Another alternative still is to duplicate your database table and name it debug table. It will have the same structure as your original. Send the test data to it instead and it won't change your original database tables.

Pedant answered 22/7, 2015 at 16:16 Comment(0)
C
1

I'm pretty sure that you've come up with some solution for your problem already.

BUT, if you don't, the Angular $httpBackend will solve your problem. It is a

Fake HTTP backend implementation suitable for unit testing applications that use the $http service.

Coburg answered 5/11, 2015 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.