I\'m trying to use factory_girl to create a \"user\" factory (with RSpec) however it doesn\'t seem to be operating transactionally and is apparently failing because of remna
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]
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.
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.
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.