我是Rails的新手。我使用FactoryGirl爲我的集成測試創建用戶,但我無法弄清楚如何在測試中登錄我的用戶。NoMethodError:未定義的方法`sign_in_as!',FactoryGirl,Rspec,Rails
我廠是這樣的:
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}@ticketee.com" }
password "password"
password_confirmation "password"
end
factory :confirmed_user do
after_create { |user| user.confirm! }
end
end
而且我的測試是這樣的:
feature 'Editing an exercise' do
before do
ex = FactoryGirl.create(:ex)
user = FactoryGirl.create(:user)
user.confirm!
sign_in_as!(user)
end
scenario 'can edit an exercise' do
visit '/'
click_link 'Exercises'
click_link 'running'
click_link 'edit'
fill_in 'Name', :with => 'kick boxing'
fill_in 'Description', :with => 'kicking a box'
click_button 'Save'
page.should have_content('Exercise updated!')
page.should have_content('kick boxing')
end
end
當我運行測試我得到的錯誤:
Failure/Error: sign_in_as!(user)
NoMethodError:
undefined method `sign_in_as!'
for #<RSpec::Core::ExampleGroup::Nested_1:0xb515ecc>
的應用效果很好,只是測試失敗。任何幫助,將不勝感激。謝謝!
是的,我正在使用Devise。我添加了c.include Devise :: TestHelpers到我的spec_helper文件,現在錯誤是失敗/錯誤:sign_in(user) NoMethodError: 未定義的方法'env'for nil:NilClass – 2013-04-30 01:51:43
我讀過了github上的devise文檔http: //github.com/plataformatec/devise再次。在測試助手部分中,聲明Devise沒有提供任何助手進行集成測試。似乎正在創建/重構「用戶登錄」測試,就像我建議的那樣。 – 2013-04-30 02:03:08