我是一个有用的助手,会为您翻译文本。
我有一个奇怪的问题...rspec在spec/routing中生成了一个名为menus_routing_spec.rb的类。
测试失败,因为menus是restaurant的嵌套资源。
这是我的测试:
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
thank you very much for help