Rockster160
Rockster160

Reputation: 1648

Rails 5 Rspec ActionController::UrlGenerationError

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.

Upvotes: 2

Views: 1900

Answers (2)

Rockster160
Rockster160

Reputation: 1648

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.

Upvotes: 1

coreyward
coreyward

Reputation: 80041

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.

Upvotes: 0

Related Questions