tim*_*one 2 rspec ruby-on-rails
这是一个采用的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'
})
Run Code Online (Sandbox Code Playgroud)
在我的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
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
{"provider"=>"facebook","uid"=>"12345","user_info"=> {"name"=>"Bob示例"}}
F
失败:
1)ServicesController OmniAuth设置会话变量到OmniAuth AUTH散列故障/错误:request.env [ "omniauth.auth"] = OmniAuth.config.mock_auth [:实] 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是因为你还没有提出任何要求.
要使测试工作,您必须做三件事:
这是我如何做到的.首先在before块中设置模拟,然后访问与提供者相对应的URL(在本例中为facebook):
before do
OmniAuth.config.add_mock(:facebook, {:uid => '12345'})
visit '/auth/facebook'
end
Run Code Online (Sandbox Code Playgroud)
来自维基:
对/ 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
Run Code Online (Sandbox Code Playgroud)
这是一个应该通过的路线和行动:
的routes.rb
match '/auth/:provider/callback' => 'sessions#callback'
Run Code Online (Sandbox Code Playgroud)
控制器/ sessions_controller.rb
def callback
session['uid'] = request.env["omniauth.auth"][:uid]
end
Run Code Online (Sandbox Code Playgroud)
这是它的要点.希望有所帮助.
| 归档时间: |
|
| 查看次数: |
1416 次 |
| 最近记录: |