Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy for testing POST to API without changing database

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?

like image 693
MattDionis Avatar asked Jul 22 '15 16:07

MattDionis


2 Answers

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

like image 123
Tyler Brown Avatar answered Oct 03 '22 21:10

Tyler Brown


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.

like image 34
cesarpachon Avatar answered Oct 03 '22 19:10

cesarpachon