如何使用rspec路由规范测试会话约束路由?

时间:2017-05-25 17:20:06

标签: ruby-on-rails ruby testing rspec

我的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'

提出手动请求

0 个答案:

没有答案