Record persisting past test in Rails3
I'm working through the railstutorial.org site and seem to have a problem with an integration test. It's suppose to check if a user gets properly created after a form post which works but, on subsequent tests fails because the test db is not getting rolled backed, this causes error because of validating that users can't have same email. Any explanation why the record would persist? If relevant the code in question is from this 开发者_运维百科listing.
Note: I am the author of the Rails Tutorial. The config.cache_classes = false
line got added after Peter Cooper reported that it was necessary to get RSpec and Spork to work together on his system. Since I have not found it necessary, and since it seemed to introduce lots of problems (such as those identified in this thread), that line has since been removed. If you use the latest version of the book you shouldn't run into this problem.
Look into using database_cleaner. Your spec helper will contain something like this:
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
ActionMailer::Base.deliveries = []
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.after(:all) do
DatabaseCleaner.clean_with :truncation
end
Seems that the issue was with the line
config.cache_classes = false
I had set this to false assuming it would make sure not to use stale class data, but it seems to be having the opposite effect among other things. Changing this to true fixed all the weirdness I was having, but I am still on confused as to why. I think it may have something to do with the OS as in the tutorial it was said that for OSX (which I am running) having that line set to true works fine, while other OSes need it set false
While Hartl's tutorial struck me as flawless, perhaps the issue you raise here can be classified as an important ommission.
This here: RailsTutorial - chapter 8.4.3 - Test database not clearing after adding user in integration test
and
This here: Rails 3 Tutorial Chapter 11 "Validation failed: Email has already been taken" error
and
This here: config.cache_classes = false messing up rspec tests?
... are all variations on the same problem.
Mike Hartl, if you are out there, you seem a mere one issue away from RoR Tutorial perfection.
Best regards,
Perry
精彩评论