【发布时间】:2012-01-15 04:46:06
【问题描述】:
缩短版:
使用 sinatra 的 omniauth gem,我无法让 rspec 登录工作并为后续请求保留会话。
根据http://benprew.posterous.com/testing-sessions-with-sinatra 的建议,并关闭会话,我已将问题隔离为:
app.send(:set, :sessions, false) # From http://benprew.posterous.com/testing-sessions-with-sinatra
get '/auth/google_oauth2/callback', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2] }
# last_request.session => {"uid"=>"222222222222222222222", :flash=>{:success=>"Welcome"}}
# last_response.body => ""
follow_redirect!
# last_request.session => {:flash=>{}}
# last_response.body => Html for the homepage, which is what I want
如何让 rspec 遵循重定向并保留会话变量?这在 Sinatra 中可行吗?
从http://benprew.posterous.com/testing-sessions-with-sinatra 看来,我必须在每个需要登录的 get/post 请求上发送会话变量,但这在重定向的情况下不起作用。
详情:
我正在尝试通过以下设置在 sinatra 中使用omniauth gem:
spec_helper.rb
ENV['RACK_ENV'] = 'test'
# Include web.rb file
require_relative '../web'
# Include factories.rb file
require_relative '../test/factories.rb'
require 'rspec'
require 'rack/test'
require 'factory_girl'
require 'ruby-debug'
# Include Rack::Test in all rspec tests
RSpec.configure do |conf|
conf.include Rack::Test::Methods
conf.mock_with :rspec
end
web_spec.rb
describe "Authentication:" do
before do
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:google_oauth2, {
:uid => '222222222222222222222',
:info => {
:email => "someone@example.com",
:name => 'Someone'
}
})
end
describe "Logging in as a new user" do
it "should work" do
get '/auth/google_oauth2/'
last_response.body.should include("Welcome")
end
end
end
在尝试进行身份验证时,我收到 <h1>Not Found</h1> 响应。我错过了什么?
在omniauth 文档的Integration testing 页面上,它提到了添加两个环境变量:
before do
request.env["devise.mapping"] = Devise.mappings[:user]
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end
但似乎只适用于轨道,正如我添加的那样
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
到我的规范中的 before 块,我收到此错误:
Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
ArgumentError:
wrong number of arguments (0 for 1)
编辑:
用
调用get
get '/auth/google_oauth2/', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2]}
似乎给我last_request.env["omniauth.auth"]等于
{"provider"=>"google_oauth2", "uid"=>"222222222222222222222", "info"=>{"email"=>"someone@example.com", "name"=>"Someone"}}
这似乎是正确的,但last_response.body 仍然返回
<h1>Not Found</h1>
【问题讨论】:
-
我找到了这个stackoverflow.com/a/3892401/111884,它似乎在进行中,但我无法让它工作。
标签: ruby testing rspec sinatra omniauth