【问题标题】:Rspec return No route matches when using scoped routeRspec返回使用范围路由时没有路由匹配
【发布时间】:2017-10-27 04:23:28
【问题描述】:

我的 routes.rb 中有一条这样定义的路线

scope ':prefix', as: :foo, controller: 'foo_paths', action: :placeholder do
  get 'foo/:id', as: 'result'
end

问题是运行我的测试时它总是返回

 Failure/Error: subject { get :placeholder }

 ActionController::UrlGenerationError:
   No route matches {:action=>"placeholder", :controller=>"foo_paths"}

这是我的所有代码,我找不到任何错误,在浏览器中一切正常,并且 rake 路由返回预期的路由。

foo_paths_controller.rb

class FooPathsController < ApplicationController
  def placeholder
    render nothing: true, status: :service_unavailable
  end
end

foo_paths_controller_spec.rb

describe FooPathsController, type: :controller do
  describe "GET 'placeholder'" do
    subject { get :placeholder }

    it 'renders an empty page with service unavailable http error' do
      subject

      it { expect(subject).to have_http_status(503) }
      it { expect(subject.body).to be_blank }
    end
  end
end

耙路

foo_result GET      /:prefix/foo/:id(.:format)                                                            foo_paths#placeholder

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rspec


    【解决方案1】:

    正如你的路线所说,它有:prefix:id。您需要在主题中指定这些内容。

    subject { get :placeholder, prefix: 1, id: 11 }
    

    我觉得下一个问题是,itit 块中不可用。所以你需要改变它。

    it 'renders an empty page with service unavailable http error' do
      subject
    
      expect(subject).to have_http_status(503)
      expect(subject.body).to be_blank
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 2019-05-25
      • 2017-04-02
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多