Reputation: 257
I have an application (API) where I wan't to test the requests for each controller execute successfully. I have the following routes:
resources :items do
resources :offers, only: [:index, :create, :destroy, :update] do
put :accept, on: :member
end
end
In the items_controller_spec.rb the following works fine:
require 'spec_helper'
describe ItemsController do
user = FactoryGirl.create(:user)
item = user.items.create()
context "GET #index" do
it 'returns unauthorised - 401 when not logged in' do
get :index
# test for the 401 unauthorised
expect(response.status).to eq(401)
end
it 'returns 200 and users items when logged in' do
get :index , { access_token: user.token }
expect(response.status).to eq(200)
end
end
end
In offers_controller_spec.rb the same code doesn't work and produces the following error:
1) OffersController GET #index shows something
Failure/Error: get :index
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"offers"}
# ./spec/controllers/offers_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
Should the request be tested in a different way because the route is nested?I tried a number of things but nothing seems to work. Any pointers would be greatly appreciated.
Upvotes: 0
Views: 852
Reputation: 36860
Because it's nested, you need to get offers for a specific item.
So you need to do
get :index, :item_id => Item.first.id
(or in place of Item.first, some specific item record you want to test against)
Upvotes: 1