我正在尝试测试一个使用oauth的Sinatra应用程序,该应用程序在每个回调路由之前运行以下代码:
before do
unless session.has_key?(:oauth_token) || request.path == '/auth/callback'
access_url = oauth_client.auth_code.authorize_url(redirect_uri: ENV['CALLBACK'])
puts "Redirecting to #{access_url}"
redirect "#{access_url}"
end
end
对于我的测试,我只是想将session[:oauth_token]
设置为 anything,这样我就可以通过这个块并进入测试。然而,经过几个小时的搜索和实验,我还没有能够弄清楚。
我试着用Rack::Test
来设置它:
describe "Visit home page", js: true do
before { get '/', {}, { 'rack.session' => { oauth_token: 'blahblahblah' } } }
it "has a list of products" do
get "/"
expect(page).to have_link("Clear & Mild Foam Handwash Refill, Fragrance-Free, 1250mL Refill, 3/Carton")
expect(page).to have_link("Coffee Portion Packs, 1.5oz Packs, Hazelnut Crème, 24/Carton")
end
end
end
我的spec_helper.rb
看起来像这样:
require File.expand_path '../../server.rb', __FILE__
require 'rspec'
require 'capybara/rspec'
require 'rack/test'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
set :environment, :test
Capybara.app = Sinatra::Application
ENV['RACK_ENV'] = 'test'
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
def setup_session(session = {})
Rack::Session::Abstract::SessionHash.stub(:new).and_return(session)
end
end
RSpec.configure do |c|
c.include RSpecMixin
end
什么是最好的方式去实际设置一个会话之前,每一个路线?
1条答案
按热度按时间iyr7buue1#
试试这个:
从这里带走的。