Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Girl Associations with Spork Discrepancy

I'm trying to use Factory Girl for RSpec with Spork. Whenever I run my tests without spork, everything passes, but when I run it with Spork, all the tests that try to create an instance of a factory that depends on another factory, fail. For example:

3) Invitation has a correctly formed body
   Failure/Error: request = FactoryGirl.create(:request, ....)
   NoMethodError:
     undefined method `user=' for #<Request:0x007f86b6a87890>

And my factories.rb code looks roughly like this

FactoryGirl.define do
  factory :user do
    sequence(:first_name) { |n| "first_name#{n}" }
    sequence(:last_name) { |n| "last_name#{n}" }
  end

  factory :request do
    association :user, :factory => :user, :is_walker => false
  end
end

My code only seems to break when there is an association, and then it tries to call the setter on the :user. Why might this be happening?

Here are the versions I'm using

gem 'rails', '3.0.7'
gem 'rspec-rails', '2.6.1'
gem 'spork', '0.9.0.rc5'
gem 'factory_girl_rails', '1.0'
like image 756
Justin Avatar asked Aug 08 '11 05:08

Justin


1 Answers

Do you have require 'factory_girl_rails in your Spork.each_run block?

I have use the following in my gemfile:

  gem "factory_girl_rails", :require => false

in spec_helper.rb:

Spork.each_run do

  require 'factory_girl_rails'
  FactoryGirl.factories.clear
  FactoryGirl.reload

end

Not sure if it will help but it's worth a shot.

like image 111
nmott Avatar answered Nov 09 '22 09:11

nmott