bendrucker / ama

Ask me questions about building web applications
MIT License
6 stars 1 forks source link

Clean up DB records during API tests #12

Closed demisx closed 9 years ago

demisx commented 9 years ago

Hi Ben, I am running my API tests using supertest package. Some of these test create records in the database that affect total counts. Is there an approach you can recommend for returning DB to previous state after each test? I realize I can probably manually delete each model that I create in test, but wondering if there is a better, more automated approach. Maybe taking a DB snapshot before each test and restoring it after each test. Can you recommend anything from your experience?

bendrucker commented 9 years ago

I use a beforeEach hook to truncate all tables and re-seed them. With especially large test suites and seed data sets that might pose problems but for my use cases it's always fine.

demisx commented 9 years ago

Yeah, I thought about that. Really miss database cleaner gem we had in Rails. :) Can you please refer me to the project where you are doing it. I'd be interested to check it out.

bendrucker commented 9 years ago

Project isn't public but I've been asked about this before and published my (very simple) test script:

https://gist.github.com/bendrucker/cf298bf515d8b6fdcd05

demisx commented 9 years ago

Thank you, sir. This helps!