集成测试与rspec和设计sign_in env [英] Integration test with rspec and devise sign_in env

查看:140
本文介绍了集成测试与rspec和设计sign_in env的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devise配置使用omniauth facebook登录整合。
从我的规范/请求测试中调用 sign_in 方法时,我得到:

I am using devise configured to use omniauth facebook sign in integration. When calling the sign_in method from my spec/request tests I get:

undefined method `env' for nil:NilClass

spec:

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 。推荐的方法是在该页面上解释,它对我有用。

Your code looks a lot like mine - I was trying to use Capybara and the Devise TestHelper functions, and it turns out you can't, per https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara. The recommended way to do it is explained on that page, and it worked for me.

要清楚,这就是我所做的 - 在 spec_helper .rb

To be clear, here's what I did - in spec_helper.rb:

RSpec.configure do |config|
  config.include Warden::Test::Helpers
end
Warden.test_mode!

而在我的代码中,只需 - logout:user

And in my code, simply - logout :user.

根据Devise wiki的说法,您不能使用 sign_out

Here's why, according to the Devise wiki, you cannot use sign_out:


如果您想知道为什么我们不能仅仅使用Devise的内建sign_in和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,我希望这不会发生。

Which, roughly, means that whereas with, say, MiniTest, an object representing the request (@request) is added as an instance variable to the test case class, that doesn't happen with Capybara. I haven't looked at the code to know the details more exactly but basically, Warden expects to find this object to then access the cookie store where the sign in credentials are. With Capybara/RSpec, I expect this isn't happening.

这篇关于集成测试与rspec和设计sign_in env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆