Reputation: 327
Running rails 3.2.3 with guard/spork/rspec/factory_girl and have the following in my spec helper:
Spork.prefork do
...
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, :type => :controller
...
end
end
And have appropriate models/factories setup so that this should work:
describe "GET index" do
describe "as logged in Person without Attendee record" do
@person = create :person
sign_in @person
it "redirects to Attendee new page" do
visit school_programs_root
current_path.should == new_school_programs_attendees
end
end
end
However, when I run the spec I get:
Exception encountered: #<NoMethodError: undefined method `create' for #<Class:0x007f860825a798>>
When I change line 3 of the spec to:
@person = FactoryGirl.create :person
The factory is created, but I get:
Exception encountered: #<NoMethodError: undefined method `sign_in' for #<Class:0x007fcee4364b50>>
All this suggests to me that the helpers are not getting loaded for my controller specs.
Upvotes: 2
Views: 1240
Reputation: 7273
There is a known issue between Spork and FactoryGirl related to class reloading. The mechanism around this that I've used for years was once documented on the Spork Wiki, but has disappeard (why? -- it seems to still be necessary). It's still documented as a FactoryGirl issue report on github.
In brief:
In Gemfile
, turn off the auto-requiring of FactoryGirl:
gem 'factory_girl_rails', '~> 3.5.0', require: false
In spec_helper.rb
, in the each_run
block, require FactoryGirl and include the Syntax Methods:
Spork.each_run do
# This code will be run each time you run your specs.
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
end
This fixes the first error. On the second error, the Devise one, you need to run sign_in
inside a before
block, see below the fixes in your example. That should work for you.
describe "GET index" do
describe "as logged in Person without Attendee record" do
before do
@person = create :person
sign_in @person
end
it "redirects to Attendee new page" do
visit school_programs_root
current_path.should == new_school_programs_attendees
end
end
end
Upvotes: 2