Why isn't factory_girl operating transactionally for me? - rows remain in database after tests

这一生的挚爱 提交于 2019-11-29 23:42:56

Finally fixed this and I hope I can save someone the six hours of debugging it took me to figure it out.

By a) getting lucky and ending up with a version of code that worked and b) stripping both sets of code down this is what I found:

Test that chokes up

require 'spec_helper'

describe UsersController do

  @user = Factory.create(:user) 
end

Test that works

require 'spec_helper'

describe UsersController do

  it "should make a factory models without choking" do
    @user = Factory.create(:user)   
  end
end

The transaction is defined by the it "should do something" do... statement. If you instantiate the factory outside that statement it turns out not to be transactional.

You can also put it outside the "it should.." block as long as it's in a "before..end" block

require 'spec_helper'

describe UsersController do

  before(:each) do
    @user = Factory.create(:user) 
  end

  it 'should make a factory without choking' do
    puts @user.name
    # prints out the correnct name for the user
  end
end

On experimenting, it seems to be valid to define a user outside of an "it should do..end" block as long as it's in a "before.. end" block. I guess this is only executed in the scope of the "it should do..end" block and therefore works fine.

[Thanks to @jdl for his (also correct) suggestion]

See my blog entry on the difference between using before :all and before :each with regard to transactions: http://mwilden.blogspot.com/2010/11/beware-of-rspecs-before-all.html. In a nutshell, before :all is not transactional, and data created there will stick around after the test is run.

In spec/spec_helper.rb, make sure you have the following

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

This seems to solve the problem for me.

Inside of test/test_helper.rb make sure that you have the following.

class ActiveSupport::TestCase
  self.use_transactional_fixtures = true
  #...
end

Despite the name "fixtures" this works with factory_girl as well.

I ran into these same symptoms when upgrading a project from Rails 3 to Rails 4. I had done a bundle install, and development mode seemed to be working fine, but I wouldn't get transactional behavior in tests. It turns out doing a bundle update solved the problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!