disabling transactional fixtures in Rspec has no effect
Due to a legacy database I'm using, I'm stuck with MySQL using MyISAM, which means my tables don't support transactions. This is causing the tests to fail, since table data ge开发者_如何学JAVAnerated (I'm using factory_girl for fixtures) by the tests are not reverted for each scenario.
I discovered that Rspec exposes the config.use_transactional_fixtures
config setting in spec_helper.rb.
which is set to true by default. When I set it to false, I don't see any effect on my tests; they still fail due to duplicate records.
Isn't that setting supposed to automatically unroll any changes made to the DB? Or am I supposed to do that manually?
You're correct - if your database doesn't support transactions you must issue several SQL commands to wipe out data before each statement:
TRUNCATE TABLE tablename;
One for each of your tables.
In your helper.rb try this:
Spec::Runner.configure do |config|
config.before(:each) do
tables = %{users posts tags}
tables.each do |t|
ActiveRecord::Base.connection.execute('TRUNCATE TABLE #{t}')
end
end
...
end
精彩评论