我正在使用配置为使用 omniauth facebook 登录集成的设备。
从我的 spec/request
测试中调用 sign_in
方法时,我得到:
undefined method `env' for nil:NilClass
规范:
describe FacebookController do
include Devise::TestHelpers
it "should display facebook logged in status" do
@user = User.create(:id => "123", :token => "token")
sign_in @user
visit facebook_path
end
end
最佳答案
您的代码看起来很像我的 - 我试图使用 Capybara 和 Devise TestHelper 函数,结果证明您不能,根据 https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara .该页面上解释了推荐的方法,它对我有用。
为了清楚起见,这是我在 spec_helper.rb
中所做的:
RSpec.configure do |config|
config.include Warden::Test::Helpers
end
Warden.test_mode!
在我的代码中,只需 - logout :user
。
根据 Devise wiki,这就是您不能使用 sign_out
的原因:
If you're wondering why we can't just use Devise's built in sign_in and sign_out methods, it's because these require direct access to the request object which is not available while using Capybara. To bundle the functionality of both methods together you can create a helper method.
粗略地说,相对于 MiniTest
,表示请求的对象 (@request
) 作为实例变量添加到测试用例类中,这不会发生在 capybara 身上。我没有查看代码以更准确地了解详细信息,但基本上,Warden 希望找到该对象,然后访问登录凭据所在的 cookie 存储。对于 Capybara/RSpec,我预计这不会发生。
关于ruby-on-rails - 使用 rspec 进行集成测试并设计 sign_in env,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7779963/