I am writing integration tests for a Rails application using Watir and RSpec. What is the best way to set up data in the database that spans each test? For instance, I have the same user I want to use across a suite of tests. How can I seed that common data (e.g. user) before all the tests run and then clean it up for the next suite of tests?
I am writing integration tests for a Rails application using Watir and RSpec. What
Share
Generally having some global data for tests is a bad idea and might introduce dependencies within tests (which is always a bad idea). To avoid that, each test should generate the data it needs for itself and not rely on some globally inserted data.
If you’re using RSpec then it does some of the job for you (it even runs your tests in a random order to avoid dependencies) – it creates a database savepoint before running the test and rollbacks all the changes done by the test. Ideally this means that test database is empty before test runs and it will be empty after test has been run.
In Rails you could use Rails own fixtures to seed the needed data or use some third party gem like FactoryGirl.
Again, do not create any global data, which will be used by all tests because sooner or later this will come back to haunt you.