Rspec返回使用作用域路由时没有路由匹配

时间:2017-10-27 04:23:28

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

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

1 个答案:

答案 0 :(得分: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