0

I have a fairly simple controller 'ActivityLogsController' in an 'Admin' namespace. When running rails server everything works as expected and I can access routes like /admin/activity_logs. When I run RSpec controller specs, every action test returns a failure. For example:

 Failure/Error: get :index

 ActionController::UrlGenerationError:
   No route matches {:action=>"index", :controller=>"activity_logs"}

OK, I admit this looks a little strange, not showing the :controller value being namespaced. That is obviously part of the issue.

The routes.rb has the activity_logs entry correctly namespaced:

 namespace :admin do
   resources :activity_logs, except: [:show, :destroy]
 end

Of course, running rake routes gives me the expected namespaced routes, which reflects the fact that the server runs correctly.

The controller is defined in 'app/controllers/admin/activity_logs_controller.rb', reflecting its namespace. It is defined as:

class Admin::ActivityLogsController < ApplicationController
   ...
end

So, why does the RSpec controller spec fail to resolve the routes correctly?

I'm posting this one with an answer, in case others have failed to find what they need in other questions.

Phil
  • 2,797
  • 1
  • 24
  • 30

1 Answers1

0

After digging for a while the queston Rspec Controllers in and out of namespace with same name gave me a clue, although was not the actual answer in my case.

My issues was that in addition to having my controller in a namespace, I had a separate controller with the same name in the controllers root. Plus to confound the issue, I also had a namespace containing other controllers, called (confusingly) activity_log

So I had:

controllers
|
|- admins
|  |-  activity_logs_controller.rb
|
|- activity_log
|  |-  other_controller.rb
|
|- activity_logs_controller.rb

So it seems that Rails auto loading worked as I would have hoped when running a server. It would put the controllers into the right places and recognise the appropriate namespaces in the routes.

But when it came to RSpec controller tests the loading was different, and somewhere deep in RSpec the activity_log subdirectory, and associated (automatically created) module (ActivityLog) containing OtherController, or the root ActivityLogsController seemed to be used to generate the Admin::ActivityLogsController routes and actions erroneously. Something in this mix presumably generated an admin/activity_logs_path that overrode the expected one.

So, even if this quick analysis of the reason for the problem is inaccurate in some way, the resolution was to move the root activity_logs_controller.rb file to another namespace. Fortunately, I was only using that controller as a super class from which I subclassed other controllers, so the move was easy. As I should have expected, having multiple things named the same just confused me, and RSpec.

Phil
  • 2,797
  • 1
  • 24
  • 30