缩短版本:如何在rspec中为sinatra使用omniauth?
使用了西纳特拉的omniauth宝石,我不能让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请求上的会话变量,但这在重定向的情况下不起作用。
细节:
我试图使用omniauth宝石西纳特拉与以下设置:
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 => "[email protected].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>
响应。我错过了什么?
在Integration testing页omniauth文档中,它提到增加了两个环境变量:
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"=>"[email protected]", "name"=>"Someone"}}
这似乎是正确的,但仍然last_response.body
返回
<h1>Not Found</h1>
我发现这个http://stackoverflow.com/a/3892401/111884,这似乎步入正轨,但我无法让它工作。 – zlog