Reputation: 4848
I've created a test case to persist data to my db. The fixtures persist correctly but the records I create in the test case aren't showing up in the database. My test looks like the following:
test "new course" do
course = Course.new
course.name = 'test'
assert course.save
end
The test passes and when I debug through it and call course.save myself I can see that it has a generated ID. Any ideas?
Upvotes: 1
Views: 1236
Reputation: 107728
If you're attempting to check that the data is in the database after the test, this won't be the case due to how the tests are run.
Tests for Rails are run inside database transactions (by default1) and so it will go ahead and create the data inside the test and run all your assertions, but once it's all said and done it's going to rollback that transaction leaving the database in a pristine state.
The reason for doing this is so that the database is always clean and so that you don't have records just lying about which could potentially alter the outcome of tests.
1 Although this can be changed by disabling the setting (I forget what this is in Test::Unit land), or by using tools like database_cleaner.
Upvotes: 7