I have some problems testing my ruby app. I have a resource restaurant and a menu of nested resources.
Route File:
resources :restaurants do
resources :menus
Menu Model:
class Menu
include Mongoid::Document
belongs_to :restaurant
accepts_nested_attributes_for :restaurant
validates :name, presence: true
validates :description, presence: true
validates :restaurant, presence: true
field :name, type: String
field :description, type: String
end
Restaurant Model:
class Restaurant
include Mongoid::Document
has_one :address, dependent: :destroy
has_many :menus, dependent: :destroy
accepts_nested_attributes_for :address, :menus
validates :name, presence: true
validates :description, presence: true
validates :address, presence: true
field :name, type: String
field :description, type: String
field :thumbnail, type: String
field :banner, type: String
end
then i try something like this. this is the default test from rspec, but which I am trying to change because I had a nested resource ..
describe MenusController do
before :each do
@restaurant = FactoryGirl.create(:random_restaurant)
@menu = FactoryGirl.create(:menu)
end
describe 'GET index' do
it 'assigns all menus as @menus' do
get restaurant_menus_path(@restaurant)
assigns(:menus).should eq([menu])
end
end
describe 'GET all restaurants menus' do
it 'responds with 200' do
get :index, { :id => @restaurant }
expect(response).to be_success
expect(response.status).to eq(200)
end
end
but the error is:
1) MenusController GET index assigns all menus as @menus
Failure/Error: get restaurant_menus_path(@restaurant)
ActionController::UrlGenerationError:
No route matches {:controller=>"menus", :action=>"/en/restaurants/52a20e4c6561721131010000/menus"}
2) MenusController GET all restaurants menus responds with 200
Failure/Error: get :index, { :id => @restaurant }
ActionController::UrlGenerationError:
No route matches {:action=>"index", :id=>"52a20e4c6561721131060000", :controller=>"menus"}
Here are the routes:
restaurant_menus_path GET (/:locale)/restaurants/:restaurant_id/menus(.:format) menus
POST (/:locale)/restaurants/:restaurant_id/menus(.:format) menus
new_restaurant_menu_path GET (/:locale)/restaurants/:restaurant_id/menus/new(.:format) menus
edit_restaurant_menu_path GET (/:locale)/restaurants/:restaurant_id/menus/:id/edit(.:format) menus
restaurant_menu_path GET (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus
PATCH (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus
PUT (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus
DELETE (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus
Interestingly, this test in menu_spec.rb works.
require 'spec_helper'
describe 'Menu' do
before :each do
@restaurant = FactoryGirl.create(:random_restaurant)
@menu = FactoryGirl.create(:menu)
end
describe 'GET /menus' do
it 'responds with 200' do
get restaurant_menu_path(@restaurant, @menu)
expect(response).to be_success
expect(response.status).to eq(200)
end
end
describe 'GET all restaurants menus' do
it 'responds with 200' do
get restaurant_menus_path(@restaurant)
expect(response).to be_success
expect(response.status).to eq(200)
end
end
end
I really don't know why this test does not work .. :(
please .. i need some explanation .. if anyone can help .. please :) reading resources are also welcome :)
many thanks..