RSpec路由规范在自定义匹配中失败



使用自定义URL匹配器进行RSpec测试失败

routes.rb:

get 'a/:code' => redirect(Rails.application.config.url_homepage)

规范/路由/routes_routing_spec.rb

describe 'routing' do
  describe "activation Urls" do
    it "redirects /a/:code to the public homepage" do
      get('/a/12341234').should route_to(Rails.application.config.url_homepage)
    end
  end
end

RSpec输出:

  1) routing activation URLS redirects /a/:code to the public homepage
     Failure/Error: get('/a/12341234').should route_to(Rails.application.config.url_homepage)
       No route matches "/a/12341234"
     # ./spec/routing/routes_routing_spec.rb:7:in `block (3 levels) in <top (required)>'

路由工作-一旦我在浏览器中打开这样的URL,我将被正确重定向。

我错过了一个重要的细节吗?

您不能将route_to匹配器与重定向路由一起使用,因为Rails处理重定向路由的方式不同。重定向路由没有控制器,其中route_to测试到/从一个控制器。

在您的情况下,您需要使用请求 spec:
describe "activation Urls" do
  before { get '/a/12341234' } 
  it { response.should redirect_to(Rails.application.config.url_homepage) }
end

最新更新