5

i have some troubles with testing my ruby on rails application. I have a resource Restaurant and a nested resource menu.

Routes 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 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..

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"}
 # ./spec/controllers/menus_controller_spec.rb:30:in `block (3 levels) in <top (required)>'

 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"}
 # ./spec/controllers/menus_controller_spec.rb:37:in `block (3 levels) in <top (required)>'

here are the routes:

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

interestingly is, that this test in menu_spec.rb is working..

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 dont know why this test is not working.. :(

please.. i need some explanation.. if anyone can help.. please :) resources to read are also welcome :)

thank you very much..

2 Answers 2

7

ok, i got it, the tests should look like this:

 it 'renders the index template' do
  get :index, { :restaurant_id => @restaurant  }
  expect(response).to render_template('index')
end

i had to add :restaurant_id.. then it worked.

Sign up to request clarification or add additional context in comments.

3 Comments

Any chance you could point the controllers? I'm running into a similar issue, but I'm getting #<Mongoid::Criteria. Dunno why!. Also, how is that on your before block you create two different instances, where do you state that both are associated?
Sorry was on vacation.. i decalred it in the models. the menu model has something like this: accepts_nested_attributes_for :restaurant and my restaurant model this: accepts_nested_attributes_for :menus
Thanks. I solved it. I don't remember what was the problem, but I believe it has to do something with the associations that must exist on the "child" resource (in the factory), or they will be treated as "unassociated" resources.
1

In test type:controllerwhen I do get restaurant_menus_path(@restaurant) that throws this error:

ActionController::UrlGenerationError:
   No route matches {:action=>"/restaurant/1/menus", :controller=>"restaurants"}

In test type controller in the method get the first argument isn't the action in the controller:

So it works with:

get :index, { :restaurant_id => @restaurant  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.