rspec 简单示例在集成测试中获取请求变量错误 [英] rspec simple example getting error on request variable in integration test

查看:36
本文介绍了rspec 简单示例在集成测试中获取请求变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个采用的 Rails 应用程序,没有测试.我正在尝试在集成测试中测试 omniauth,但出现错误(编辑 我基于此:https://github.com/intridea/omniauth/wiki/Integration-Testing).这反映了我对 Rspec 缺乏了解.看起来请求对象默认是可用的.

This is an adopted rails app with no tests. I am trying to test omniauth in an integration test but am getting an error (edit I have based upon this: https://github.com/intridea/omniauth/wiki/Integration-Testing). This reflects my lack of understanding of Rspec. It would seem that the request object would be available by default.

我的规范/spec_helper.rb 中有:

I have in my spec/spec_helper.rb:

config.include IntegrationSpecHelper, :type => :request
Capybara.default_host = 'http://localhost:3000'

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {
  :uid => '12345'
})

在我的规范/integration/login_spec中:

and in my spec/integration/login_spec:

require 'spec_helper'

describe ServicesController, "OmniAuth" do
  before do
    puts OmniAuth.config.mock_auth[:facebook]
    puts request # comes back blank
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
  end

  it "sets a session variable to the OmniAuth auth hash" do
    request.env["omniauth.auth"][:uid].should == '12345'
  end
end 

我收到以下错误:

{"provider"=>"facebook", "uid"=>"12345", "user_info"=>{"name"=>"Bob示例"}}

{"provider"=>"facebook", "uid"=>"12345", "user_info"=>{"name"=>"Bob Example"}}

F

失败:

1) ServicesController OmniAuth 将会话变量设置为OmniAuth 身份验证哈希失败/错误:request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]无方法错误:未定义的方法 env' for nil:NilClass# ./login_spec.rb:8:in '

1) ServicesController OmniAuth sets a session variable to the OmniAuth auth hash Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] NoMethodError: undefined method env' for nil:NilClass # ./login_spec.rb:8:inblock (2 levels) in '

在 22.06 秒内完成 1 个示例,1 个失败

Finished in 22.06 seconds 1 example, 1 failure

失败的例子:

rspec ./login_spec.rb:11 # ServicesController OmniAuth 设置会话OmniAuth 身份验证哈希的变量

rspec ./login_spec.rb:11 # ServicesController OmniAuth sets a session variable to the OmniAuth auth hash

默认情况下,请求对象应该在这里可用吗?这个错误可能意味着其他什么吗?

Should the request object be available here, by default? Does this error possibly mean something else?

谢谢

推荐答案

你得到 nil 因为你还没有提出任何请求.

You're getting nil because you haven't made any request yet.

要使测试有效,您必须做三件事:

To make the test work, you have to do three things:

  1. 设置模拟
  2. 提出请求
  3. 测试附加到回调的任何代码

这就是我的做法.首先在 before 块中设置 mock,然后访问提供者对应的 URL(在本例中为 facebook):

Here's how I do it. First set up the mock in the before block, and then visit the URL corresponding to the provider (in this case facebook):

before do
  OmniAuth.config.add_mock(:facebook, {:uid => '12345'})
  visit '/auth/facebook'
end

来自 wiki:

对/auth/provider 的请求将立即重定向到/auth/provider/callback.

A request to /auth/provider will redirect immediately to /auth/provider/callback.

所以你必须有一个匹配'/auth/:provider/callback'的路由.您映射的任何操作都必须执行上述步骤 3 中的内容.

So you have to have a route which matches '/auth/:provider/callback'. Whatever action you map that do has to perform the stuff in step 3 above.

如果您想测试会话变量是否设置为 uid,您可以执行以下操作(之所以有效,是因为您在上面的模拟中将 uid 设置为12345"):

If you wanted to test that the session variable was set to the uid, you could do something like this (which works because you set the uid to '12345' in the mock above):

it "sets a session variable to the OmniAuth auth hash" do
  session['uid'].should == '12345'
end

这是应该通过的路线和动作:

And here's a route and action that should make this pass:

routes.rb

match '/auth/:provider/callback' => 'sessions#callback'

controllers/sessions_controller.rb

controllers/sessions_controller.rb

def callback
  session['uid'] = request.env["omniauth.auth"][:uid]
end

这就是它的要点.希望有所帮助.

That's the gist of it. Hope that helps.

这篇关于rspec 简单示例在集成测试中获取请求变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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