ruby-on-rails - omn​​iauth-facebook 和使用 rspec 进行测试

标签 ruby-on-rails rspec omniauth

我正在尝试测试omniauth。我已经放入 config/environments/test.rb

OmniAuth.config.test_mode = true

在我的spec_helper.rb中

OmniAuth.config.add_mock(:facebook,
                             { :provider => 'facebook',
                               :uid => '1234567',
                               :info => { :first_name => 'Jonathan', :name=> 'Jonathan', :email => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5451507e545f55105d5153" rel="noreferrer noopener nofollow">[email protected]</a>', :image => 'http://graph.facebook.com/1234567/picture?type=square'},
                               :credentials => {
                                 :expires_at => 1351270850,
                                 :token=> 'AAADzk0b791YBAHCNhBI3n6ScmWvuXTY4yIUqXr9WiZCg1R808RYzaHxsHnrbn62IwrIgZCfSBZAVIP6ptF41nm8YtRtZCeBbxbbz1mF8RQZDZD'
                                } })

然后在我的请求 Controller 中:

it "testing omniauth hash" do
  visit '/auth/facebook'
  request.env["omniauth.auth"][:uid].should == '1234567'
end

并得到:

Failures:

  1) Login logins with the OmniAuth auth hash
     Failure/Error: request.env["omniauth.auth"][:uid].should == '1234567'
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/requests/login_spec.rb:38:in `block (2 levels) in <top (required)>'

Finished in 2.4 seconds
9 examples, 1 failure

知道发生了什么吗?提前谢谢

编辑 1 enter image description here

最佳答案

编辑:实际上,我重现了该错误,问题似乎是请求对象不会被实例化,除非您使用 [http_verb] [url] 发出 native 请求。因此,如果您使用 get '/auth/facebook' 而不是 visit '/auth/facebook',它应该可以工作。

关于ruby-on-rails - omn​​iauth-facebook 和使用 rspec 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12575610/

相关文章:

ruby-on-rails - ruby on rails,创建新对象,使用创建或新方法?

ruby-on-rails - 在 Ruby on Rails 3.1+ 中公开静态文件?

Rspec 和 watir-webdriver;断言元素不存在

ruby-on-rails - Rails 4 混淆了我的一个引擎模型与另一个引擎库模块

ruby-on-rails - 代表 has_many :though with Factory Bot

ruby-on-rails - JSON 参数在请求哈希中不可用(Rails、omniauth-google-oauth2 gem)

ruby-on-rails - 将 Rails 应用程序与 Google Apps 集成

ruby-on-rails - Cucumber 将 pdf 读入临时文件

mysql - 在没有 Arel 的情况下如何在 Rails 中编写此查询

ruby-on-rails - rails : retrieving image from Facebook after Omniauth login with Devise