0
votes

Given this contrived example:

RSpec.describe City::Road::HouseController, type: :controller do
  let(:house) { create :house }

  describe 'index' do    
    context 'a request is received without a valid basic-auth header' do
      it 'renders unauthorized' do
        before { method_that_unsets_basic_auth_header }
        get(:index, params: { house_id: house.id })

        expect(response).to have_http_status(:unauthorized)
      end
    end
  end
end

with the following routes:

namespace :city do
  namespace :road do
    resources :location, only: %i[show index] do
      resources :house, only: %i[index], on: :member
    end
  end
end

Why do I keep getting this error?:

 ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"city/road/house :house_id=>1}

I think it's because it's ignoring location.

I expect the route to be:

/city/road/location/:location_id/house

I've looked at the following questions/answers but none of them helped (though I admit I may have missed something):

  1. How to test controllers with nested routes using rspec
  2. Rails Rspec controller testing with nested resources
  3. RSpec controller for nested resources, before_action
1
What exactly do you expect the route to be? - anothermh
AH, oops, thanks for the catch! - Thermatix

1 Answers

0
votes

I took another look at the other questions/answers I linked and I noted that they all had one difference then what I had.

I removed the :member option

namespace :city do
  namespace :road do
    resources :location, only: %i[show index] do
      resources :house, only: %i[index]
    end
  end
end

and the spec now passess green, I don't know why that would affect the spec epecily becuase the route looks the same with or without that option.