我正在尝试在我的某个控制器上编写自定义操作的请求规范。
我的routes.rb是这样的:
resources :cars do
member do
get :search
end
end
我的cars_controller.rb是这样的:
class CarsController < ApplicationController
def search
# pending
end
end
我的controller_cars_spec.rb是这样的:
describe CarsController do
describe 'Search' do
it 'should call the model method that perform search by brand' do
get :search
end
end
end
佣金路线说:
search_car GET /cars/:id/search(.:format) {:action=>"search", :controller=>"cars"}
并且自动测试说:
Failures:
1) CarsController Search should call the model method that perform search by brand
Failure/Error: get :search
ActionController::RoutingError:
No route matches {:controller=>"cars", :action=>"search"}
# ./spec/controllers/cars_controller_spec.rb:19:in `block (3 levels) in <top (required)>'
有什么问题?我无法解决这个失败......
答案 0 :(得分:1)
您需要将一个:id参数传递给spec文件中的路由,如本文所示: "No route matches" error?
get :search, :id => @car.id
答案 1 :(得分:0)
member
用于单个资源,因此它需要:id。你想要:
resources :cars do
collection do
get :search
end
end
答案 2 :(得分:0)
检查是否
resources :cars
之前
match '/cars/:id/search' => 'cars#search'
答案 3 :(得分:0)
简而言之,这应该做你想做的事:
get :search, :car_id => @car.id
当您使用资源时,您需要匹配每个参数的路线,但这并不完全是故事的全部内容。类似的东西:
get :search, :id => @car.id
似乎它会起作用,但不会。
使用资源帮助程序时,通过为任何子路由和资源添加前缀资源名称来命名ID。我不完全确定为什么rspec错误并没有抱怨丢失:car_id,也许其他人可以填写缺少的部分知识。