縮短版本:如何在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