我的routes.rb中有这样的代码:
Rails.application.routes.draw do
authorized = ->(request) { request.session[:user_id].present? }
not_authorized = ->(request) { request.session[:user_id].blank? }
constraints authorized do
resources :users
end
constraints not_authorized do
get 'login' => 'auth#login_page'
post 'login' => 'auth#create_session'
get '*unmatched_route', to: 'auth#login_page'
root 'auth#login_page'
end
end
我有这样的users_routing_spec.rb
档案:
要求" rails_helper"
RSpec.describe UsersController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/users").to route_to("users#index")
end
end
end
此测试因路由到'auth#login_page'
而失败,因为没有user_id
在会议中。
如何提前致电auth#create_session
预期?
没有request
或@request
对象,我也无法向#aut; #creal_session'