FactoryGirl and Devise helpers not working in specs-Collection of common programming errors

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 

Originally posted 2013-11-09 21:23:15.