【发布时间】:2014-02-04 10:51:22
【问题描述】:
我有一个奇怪的问题.. rspec 在 spec/routing 中生成了一个名为 menus_routing_spec.rb 的类
测试失败是因为 menus 是餐厅的嵌套资源。
这是我的测试:
describe MenusController do
before :each do
@restaurant = FactoryGirl.create(:random_restaurant)
@menu = FactoryGirl.create(:menu)
end
describe 'routing' do
it 'routes to #index' do
params = {}
params['restaurant_id'] = @restaurant
#get('/restaurants/:restaurant_id/menus').should route_to('menus#index')
#get(restaurant_menus_path(@restaurant)).should route_to('menus#index')
#get(restaurant_menus_path, { :restaurant_id => @restaurant }).should route_to('menus#index')
get restaurant_menus_path, { :restaurant_id => @restaurant.to_param }
expect(response).should route_to('menus#index')
end
rake 路由中的路径如下所示:
restaurant_menus_path GET (/:locale)/restaurants/:restaurant_id/menus(.:format) menus#index
我总是收到此错误消息:
Failure/Error: get restaurant_menus_path, @restaurant.to_param
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"menus"} missing required keys: [:restaurant_id]
我也试过其他的..但同样的错误.. 有谁可以看到我在哪里做错了?
这是 spec/controllers/menus_controller_spec.rb 中的一个测试,效果很好
it 'renders the index template' do
get :index, { :restaurant_id => @restaurant }
expect(response).to render_template('index')
end
非常感谢您的帮助
【问题讨论】:
标签: ruby-on-rails ruby rspec param nested-resources