使用guard / spork / rspec / factory_girl运行rails 3.2.3并在我的规范助手中有以下内容:
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
然而,当我运行规范时,我得到:
Exception encountered: #<NoMethodError: undefined method `create' for #<Class:0x007f860825a798>>
当我将规范的第3行更改为:
时@person = FactoryGirl.create :person
工厂已创建,但我得到了:
Exception encountered: #<NoMethodError: undefined method `sign_in' for #<Class:0x007fcee4364b50>>
所有这些都告诉我,帮助器没有为我的控制器规格加载。
答案 0 :(得分:2)
Spork和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
这解决了第一个错误。在第二个错误,设计一个,您需要在sign_in
块内运行before
,请参阅下面的示例中的修复程序。这应该适合你。
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
答案 1 :(得分:-1)
添加到您的规范:
include Devise::TestHelpers