2012-04-16 45 views
2

運行軌道3.2.3後衛/叉勺/ RSpec的/ factory_girl並在我的規格幫手如下:FactoryGirl並設計助手不規範工作

Spork.prefork do 
    ... 
    RSpec.configure do |config| 
    config.include FactoryGirl::Syntax::Methods 
    config.include Devise::TestHelpers, :type => :controller 
    ... 
    end 
end 

並有適當的模型/工廠設置,使這應工作:

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 

然而,當我運行spec我得到:

Exception encountered: #<NoMethodError: undefined method `create' for #<Class:0x007f860825a798>> 

當我改變李規範的NE 3:

@person = FactoryGirl.create :person 

工廠創建的,但我得到:

Exception encountered: #<NoMethodError: undefined method `sign_in' for #<Class:0x007fcee4364b50>> 

所有這些都表明,我認爲傭工沒有得到加載我的控制器的規格。

回答

2

有叉勺和FactoryGirl相關類重載之間的已知問題。我已經使用多年的機制曾經在Spork Wiki上記錄過,但已經消失了(爲什麼? - 它似乎仍然是必要的)。它仍然記錄爲FactoryGirl issue report on github

簡單:

Gemfile,關閉自動要求FactoryGirl的:

gem 'factory_girl_rails', '~> 3.5.0', require: false 

spec_helper.rb,在each_run塊,需要FactoryGirl和包括語法方法:

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 

修復了第一個錯誤。在第二個錯誤中,設計一個,您需要在before塊內運行sign_in,請參閱下面的示例中的修復。這應該適合你。

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 
+0

我不再有這些測試,所以不能在我的代碼上測試您的建議。感謝您爲將來可能使用它的任何人提供幫助。 – iliveinapark 2012-06-26 00:49:09

-1

添加到您的規格:

include Devise::TestHelpers 
+1

不幸的是沒那麼簡單;雖然它推過了sign_in錯誤,但它現在甚至變得更奇怪(http://pastebin.com/ME302FrB) – iliveinapark 2012-04-16 08:59:40

+0

我不確定,但是如果將sign_in放在之前:每個做結束塊? – gayavat 2012-04-16 10:33:13

+0

沒有什麼區別。我最終只使用了詳細的語法,並使用Cucumber功能來測試。謝謝你的幫助。 – iliveinapark 2012-04-18 02:57:26