title: Ruby on Rails 4: test nested resources with rspec not working
body: <p>i have some troubles with testing my ruby on rails application. I have a resource Restaurant and a nested resource menu. </p>
  
  <p>Routes file:</p>
  
  <pre><code>resources :restaurants do
    resources :menus
  </code></pre>
  
  <p>Menu model:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>restaurant model:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>then, i am trying something like this. it is a default test from rspec, but which i am trying to modify because i had a nested resource..</p>
  
  <pre><code>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 =&gt; @restaurant  }
    expect(response).to be_success
    expect(response.status).to eq(200)
    end
  end
  </code></pre>
  
  <p>but the error is:</p>
  
  <pre><code>  1) MenusController GET index assigns all menus as @menus
   Failure/Error: get restaurant_menus_path(@restaurant)
   ActionController::UrlGenerationError:
     No route matches {:controller=&gt;"menus", :action=&gt;"/en/restaurants/52a20e4c6561721131010000/menus"}
   # ./spec/controllers/menus_controller_spec.rb:30:in `block (3 levels) in &lt;top (required)&gt;'
  
   2) MenusController GET all restaurants menus responds with 200
   Failure/Error: get :index, { :id =&gt; @restaurant  }
   ActionController::UrlGenerationError:
     No route matches {:action=&gt;"index", :id=&gt;"52a20e4c6561721131060000", :controller=&gt;"menus"}
   # ./spec/controllers/menus_controller_spec.rb:37:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>here are the routes:</p>
  
  <pre><code>restaurant_menus_path    GET     (/:locale)/restaurants/:restaurant_id/menus(.:format)   menus#index
  POST     (/:locale)/restaurants/:restaurant_id/menus(.:format)   menus#create
  new_restaurant_menu_path     GET     (/:locale)/restaurants/:restaurant_id/menus/new(.:format)   menus#new
  edit_restaurant_menu_path    GET     (/:locale)/restaurants/:restaurant_id/menus/:id/edit(.:format)  menus#edit
  restaurant_menu_path     GET     (/:locale)/restaurants/:restaurant_id/menus/:id(.:format)   menus#show
  PATCH    (/:locale)/restaurants/:restaurant_id/menus/:id(.:format)   menus#update
  PUT  (/:locale)/restaurants/:restaurant_id/menus/:id(.:format)   menus#update
  DELETE   (/:locale)/restaurants/:restaurant_id/menus/:id(.:format)   menus#destroy
  </code></pre>
  
  <p>interestingly is, that this test in menu_spec.rb is working..</p>
  
  <pre><code>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
  </code></pre>
  
  <p>i really dont know why this test is not working.. :(</p>
  
  <p>please.. i need some explanation.. if anyone can help.. please :)
  resources to read are also welcome :)</p>
  
  <p>thank you very much..</p>