rspec简单示例在集成测试中获取请求变量的错误
这是一个采用的rails应用程序,没有测试。 我试图在集成测试中测试omniauth但是我收到了一个错误( 编辑我基于此: https : //github.com/intridea/omniauth/wiki/Integration-Testing )。 这反映了我对Rspec缺乏了解。 似乎默认情况下请求对象可用。
我有我的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' })
在我的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示例”}}
F
失败:
1)ServicesController OmniAuth将会话变量设置为OmniAuth auth哈希失败/错误:request.env [“omniauth.auth”] = OmniAuth.config.mock_auth [:facebook] NoMethodError:未定义的方法
env' for nil:NilClass # ./login_spec.rb:8:in
块(2级)中’完成22.06秒1例,1次失败
失败的例子:
rspec ./login_spec.rb:11#ServicesController OmniAuth将会话变量设置为OmniAuth auth哈希
请问对象在这里是否可用,默认情况下? 这个错误可能意味着别的吗?
谢谢
因为你还没有提出任何要求,你的结果nil
。
要使测试工作,您必须做三件事:
- 设置模拟
- 提出要求
- 测试附加到回调的任何代码
我就是这样做的。 首先在前一个块中设置模拟,然后访问与提供者相对应的URL(在本例中为facebook):
before do OmniAuth.config.add_mock(:facebook, {:uid => '12345'}) visit '/auth/facebook' end
来自维基 :
对/ auth / provider的请求将立即重定向到/ auth / provider / callback。
所以你必须有一个匹配’/ auth /:provider / callback’的路由。 无论您采取何种行动,都必须执行上述步骤3中的操作。
如果你想测试会话变量是否设置为uid,你可以做这样的事情(这是有效的,因为你在上面的模拟中将uid设置为’12345’):
it "sets a session variable to the OmniAuth auth hash" do session['uid'].should == '12345' end
这是一个应该通过的路线和行动:
的routes.rb
match '/auth/:provider/callback' => 'sessions#callback'
控制器/ sessions_controller.rb
def callback session['uid'] = request.env["omniauth.auth"][:uid] end
这就是它的要点。 希望有所帮助。