使用UrlGenerationError在Rails控制器规范中路由失败

时间:2016-01-19 10:47:42

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

我没有通过rspec控制器而导致路由错误。

假设我在routes.rb中定义了以下路由:

Rails.application.routes.draw do
resources :models, only: [:show] do
  post ':model_slug/model_types_price/:model_types_slug' => 'models#model_types_price', on: :collection
end

在models_controller_spec.rb中,我想像这样测试#model_types_price动作:

describe "POST #model_types_price" do
let(:params) { {
    model_slug: "bmw-serie-1",
    model_type_slug: "s-330i1",
    price: "67.31"
} }
end
it 'assigns a model one of its types' do
  post :model_types_price, params, valid_session
  expect(assigns(:model)).to eq(model)
end

该示例未通过以下错误:

  

的ActionController :: UrlGenerationError:          没有路线匹配{:action =>" model_types_price",:base_price =>" 12.78",:controller =>" models",:model_slug = >" bmw-serie-1-2323" ,: model_type_slug =>" s-330i1"}

然而,当在浏览器中测试网址时,它可以工作:

http://localhost:3000/models/bmw-serie-1-2323/model_types_price/s-330i1

任何想法如何映射修复此问题? 提前致谢

0 个答案:

没有答案