2

Searched through other similar questions on here, and they all seem to be related to a malformed request of some sort (Not providing an id, or not including the params correctly) but those don't seem to be the problem I'm having.

The route definitely exists, going to the route in the browser works just fine. Page loads, everything. But Rspec is giving me a UrlGenerationError for some reason.

I've tried manually specifying the controller name in the routes, changing to a pluralized controller, and even using a different (pluralized) controller. It's seeming like there is an issue with some other configuration somewhere, but the error that it's a URLGeneration error is extremely unhelpful. I would greatly appreciate any other ideas.

We have API controller specs elsewhere in our app that seem to be functioning correctly, but I cannot tell the difference in set up between those and this one.

My error:

  1) Spaceman::ReputationEnhancementsController GET show has a 200 status code
     Failure/Error: get :show

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"spaceman/reputation_enhancements"}
     # ./spec/controllers/spaceman/reputation_enhancements_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 1.14 seconds (files took 6.13 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/spaceman/reputation_enhancements_controller_spec.rb:9 # Spaceman::ReputationEnhancementsController GET show has a 200 status code

Routes:

Spaceman::Engine.routes.draw do
  resource :reputation_enhancement, path: "reputation-enhancement", only: [ :show, :create ] do
    get :filter
  end
end

Test: (I've tried added type: :controller here, as well as fully qualifying, adding Rspec.describe instead, etc...)

require "rails_helper"

describe Spaceman::ReputationEnhancementsController do

  describe "GET show" do
    it "has a 200 status code" do
      get :show
      expect(response.status).to eq(200)
    end
  end

end

rake routes

filter_reputation_enhancement GET    /reputation-enhancement/filter(.:format) spaceman/reputation_enhancements#filter
       reputation_enhancement GET    /reputation-enhancement(.:format)        spaceman/reputation_enhancements#show
                              POST   /reputation-enhancement(.:format)        spaceman/reputation_enhancements#create

EDIT:

I've tried manually specifying the controller name in the routes, changing to a pluralized controller, and even using a different (pluralized) controller. It's seeming like there is an issue with some other configuration somewhere, but the error that it's a URLGeneration error is extremely unhelpful. I would greatly appreciate any other ideas.

1

2 Answers 2

1

Turns out, the issue was that the Controller was part of a gem and the routes to access the controller were not included by default.

Solved by adding routes { Spaceman::Engine.routes } to the top inside of the main describe block.

It's all fine to leave singular routes and everything else was set up correctly. Just had to include the routes.

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

2 Comments

Thanks, routes { Rails.application.routes } works. Is there a convenient way to do this? It's annoying to add this for all describe block.
Hm. I didn't look much deeper into, I'm sorry. I do image there is a way to configure some sort of option in rspec to set that automatically.
0

You're using the singular resource in your routes but your spec is requesting the plural form “reputation_enhancements”.

See this bit in the routing guide for singular resources and this long standing issue in the Rails issue tracker that explains that there's no good way to get url_for to map back to a singular resource.

To resolve it, either specify the path in your spec or add a plural route.

5 Comments

Where is the spec requesting the plural form? The controller is supposed to be pluralized even with a singular route, if that's what you were referring to- so I'm not sure where the pluralization is messed up?
Switched all of the references to the Controller to singular (by manually assigning the controller in the routes) and the problem still exists.
@Rockster160 I added some more info for you.
I'm having the same issue using plural resources. (Switched to a different controller to try to test, but it's still failing with the same error)
Is there a way to specify the path in my spec? I can't find a way to manually add in a path. I've made the route plural, I've tried making the controller singular, I've read through the other post you commented and didn't make any progress. (their issue was, again, invalid routing syntax- like most of the other posts I've seen that are similar to this one)

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.