dam*_*mir 4 ruby rspec ruby-on-rails param nested-resources
我有一个奇怪的问题.. rspec在spec/routing中生成了一个名为menus_routing_spec.rb的类
测试失败,因为菜单是餐馆的嵌套资源.
这是我的测试:
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
Run Code Online (Sandbox Code Playgroud)
rake路由中的路径如下所示:
restaurant_menus_path GET (/:locale)/restaurants/:restaurant_id/menus(.:format) menus#index
Run Code Online (Sandbox Code Playgroud)
我总是收到此错误消息:
Failure/Error: get restaurant_menus_path, @restaurant.to_param
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"menus"} missing required keys: [:restaurant_id]
Run Code Online (Sandbox Code Playgroud)
我也尝试了其他的..但同样的错误..有谁能看到我在做错误的地方?
这是spec/controllers/menus_controller_spec.rb中的测试,它可以正常工作
it 'renders the index template' do
get :index, { :restaurant_id => @restaurant }
expect(response).to render_template('index')
end
Run Code Online (Sandbox Code Playgroud)
非常感谢你的帮助
路由规范应该测试get给定路径作为字符串的action()(即"/ first/1/second/2")将路由到具有正确参数集的动作(即first_id: 1, id: 2)
您无需在此处创建模型实例.这是不必要的,它只会减慢规格.
describe MenusController do
describe 'routing' do
it 'routes to #index' do
get('/restaurants/42/menus').should route_to('menus#index', restaurant_id: 42)
end
it 'routes to #show' do
get('/restaurants/42/menus/37').should route_to('menus#index', restaurant_id: 42, id: 37)
end
end
end
Run Code Online (Sandbox Code Playgroud)
您还可以传入其他参数,例如format: :json,或者可能从URL字符串中收集的任何其他参数,因为它主要测试您的路径文件使用正确的参数将您引导到正确的位置.
| 归档时间: |
|
| 查看次数: |
3118 次 |
| 最近记录: |