【问题标题】:omniauth-facebook and testing with rspecomn​​iauth-facebook 和使用 rspec 进行测试
【发布时间】:2013-01-15 07:01:49
【问题描述】:

我正在尝试测试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 => 'jon@jak.com', :image => 'http://graph.facebook.com/1234567/picture?type=square'},
                               :credentials => {
                                 :expires_at => 1351270850,
                                 :token=> 'AAADzk0b791YBAHCNhBI3n6ScmWvuXTY4yIUqXr9WiZCg1R808RYzaHxsHnrbn62IwrIgZCfSBZAVIP6ptF41nm8YtRtZCeBbxbbz1mF8RQZDZD'
                                } })

然后在我的请求控制器中:

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

【问题讨论】:

    标签: ruby-on-rails rspec omniauth


    【解决方案1】:

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

    【讨论】:

    • 谢谢,但是……嗯……同样的错误。 OmniAuth.config.test_mode = true 应该在环境文件还是 spec_helper.rb 中?
    • hmmm... 仍然遇到同样的错误。我在上面添加了一个屏幕截图。不确定,但谢谢帮助
    • 我对omniauth不太熟悉,但是只看上面的railscast,似乎“omniauth.auth”只有在应用程序从身份验证返回时才被设置,这可以解释为什么@987654323 @ 是零。您是否尝试在代码中的其他任何地方解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2012-01-06
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多