Namespaced controller load order
Reported by Kyle Hargraves | November 25th, 2007 @ 10:30 PM
Originally from rspec-users in Scott Taylor's 11/23/2007 "namespaced controllers" thread:
Given the files:
spec/controllers/foo_controller_spec.rb
spec/controllers/admin/foo_controller_spec.rb
The specs do nothing but hit the FooController#show and Admin::FooController#index actions and expect the right render.
If I touch admin/foo_controller_spec.rb, the specs pass.
If I touch foo_controller_spec.rb, the specs for Admin::FooController fail with UnknownAction exceptions, since ::FooController has no index action defined.
Problem occurs with autotest and rake spec, but not with ./script/spec spec.
This is happening with the current latest trunk versions, rails r8200 and rspec r2980.
I have no idea how to express this with a failing spec in the example rails app, sorry.
Comments and changes to this ticket
-
David Chelimsky November 26th, 2007 @ 03:32 AM
- → State changed from new to open
Kyle - this sounds like an issue related to the fact that autotest has already loaded the environment each time it runs. Have you tried the same thing using test/unit instead of rspec? I'd be surprised if the results are any different. Not shocked, mind you :) - just surprised.
-

-
-

Kyle Hargraves December 13th, 2007 @ 02:07 AM
Reminded about this by the ML thread.
The problem may not actually be with rspec; could Scott/anyone else confirm that this started happening without updating Rails at the same time? We're all chasing Rails edge most of the time, so I doubt anyone does that much.
When I tried to figure this one out, I ended up printing the described type that the example groups eventually receive. It was ::FooController rather than Admin::FooController, but AFAIK ruby has no means of 'resolving' a class to some module within which it resides. It's inherently fully defined, right?
Which I think implies that the issue is rails' const_missing or whathaveyou doesn't recognize that it's being called within a module, and searches for ::FooController. A guess, I haven't looked into Rails to find out.
-
Scott Taylor December 13th, 2007 @ 02:48 AM
Just to get back to Kyle: I haven't been chasing rails trunk; I've been on rails 1.2.3 frozen, the whole time.
-

Kyle Hargraves December 13th, 2007 @ 02:56 AM
So much for that idea.
I'll try and tinker with git-bisect and figure out when this started happening, then. I've been looking for an excuse to use it more anyhow.
-

Kyle Hargraves December 13th, 2007 @ 03:36 AM
This exists as far back as r1800. Reverting further beyond that is probably not worth it.
I guess a fix would be to always go as deep into spec/controllers as possible and then load back up the directory stack, but that seems pretty lame.
Using Admin::FooController is perfectly acceptable, no? For that matter, if people insist on putting things in modules, they can just put the fully qualified name on the line immediately before opening the module and everything's rainbows.
-

harm January 2nd, 2008 @ 02:00 PM
I'm having the same problem.
I have two controllers:
one in the default namespace: DevicesController
the second one is in the reseller namespace.
i'm referencing them in my example files as follows:
describe DevicesController do
end
and:
describe Reseller::DevicesController do
end
However when I save the examples in the default namespace, all the examples in the reseller namespace fail. When I 'touch' the file in the reseller namespace everything is back to normal and all examples pass.
Is this a bug? Is there a workaround available? I have lookup into the rspec source code, but could not find the source of the problem.
-
Thijs January 9th, 2008 @ 03:50 PM
I'm having the same problems while using the describe Reseller::DevicesController do form to.
It does look like this might be an issue in Rails itself. Did anybody find any clues?
-
Scott Taylor January 9th, 2008 @ 04:30 PM
I'm no longer experiencing the bug with rails 2.0.2 and rspec 1.1.1, and ruby 1.8.6 (patchlevel 0). What are you guys running (I'm surprised Aslak hasn't chimed in with his boiler-plate message yet)?
-
Thijs January 9th, 2008 @ 04:38 PM
I'm running ruby 1.8.6 (2007-03-13 patchlevel 0), Rails 2.0.2 gem and the newest CURRENT rspec in vendor/plugins. I still get the same errors.
-
Thijs January 27th, 2008 @ 04:27 PM
Does anybody have any ideas about this issue? Or is it solved for most people?
-
Jason L Perry March 19th, 2008 @ 04:50 PM
RSpec 1.1.3, Rails 2.0.2, Ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]
And I still have this issue.
I'm not sure what kyle means by:
Using Admin::FooController is perfectly acceptable, no? For that matter, if people insist on putting things in modules, they can just put the fully qualified name on the line immediately before opening the module and everything's rainbows.
:( Everything is not rainbows.
/cry
-
Jason L Perry March 19th, 2008 @ 05:01 PM
ok so grok this:
I did nothing but change:
require File.dirname(__FILE__) + '/../../spec_helper' describe Admin::SchoolsController doto:
require File.dirname(__FILE__) + '/../../spec_helper' Admin::SchoolsController describe Admin::SchoolsController doAnd 'rake spec' passes all examples again. I removed the line, and they still continued to pass. shrug I'm go to stop thinking about this now.
-
Robby Russell May 16th, 2008 @ 07:25 PM
I'm seeing the same behaviour here as well. Added
Admin::UsersController
to my controller spec and `rake spec` works again.
-
Thijs May 16th, 2008 @ 07:52 PM
I've noticed that this problem mainly pops up when you have a model with the same name that a namespace has.
Do you have a model called Admin?
-
Robby Russell May 16th, 2008 @ 07:56 PM
I've noticed that this problem mainly pops up when you have a model with the same name that a namespace has.
Do you have a model called Admin?
Indeed! We do have an Admin model.
-

Kyle Hargraves May 16th, 2008 @ 08:12 PM
I have never had an Admin model on a project, and it's always the Admin namespace that bites me. =\
-

Scott Taylor May 16th, 2008 @ 08:25 PM
In reply to Jason's comment above,
I thought that the following would work:
describe Admin::FooBarController { ... }
but the following wouldn't:
module Admin
describe FooBarController { ... }
end
Am I mistaken? If I'm correct, than I would expect this is about load order regarding Rails using Module.autoload.
-

Phil Orwig July 1st, 2008 @ 05:41 PM
- → Tag changed from to bug namespaces rails
Is there anything I can do to help this along? I'm running into the same issue with namespaced controllers, similar to the usual Admin issue listed here.
This is with rspec 1.1.4 and Rails 2.1.
-
-

Phil Orwig July 1st, 2008 @ 06:17 PM
No -- in fact, everything here is namespaced under Analyst, and I don't have an Analyst model.
-
David Chelimsky July 1st, 2008 @ 06:19 PM
- → Assigned user changed from to David Chelimsky
-
Tony Perrie August 6th, 2008 @ 07:35 PM
I'm seeing the same issue here. We have a model called Admin, however, removing that model seems not to resolve the issue. As a workaround, I just added a require for the particular controller in the RSpec test itself. I only see this issue occurring on Ubuntu Linux and not OS X. Is this perhaps something to do with the order that the filesystem returns the list of controllers (HPFS vs ext3)?
-
Robby Russell August 11th, 2008 @ 07:55 PM
Tony,
One way around this is to name your model Administrator. There was some conflicts in Rails as well with the namespacing. So, moving to calling the model Administrator (instead of the abbreviated Admin) did wonders.
Robby
-
Tony Perrie August 12th, 2008 @ 05:03 AM
Robby,
I've actually tried removing the model already, and that seemed not to have an effect. I can try renaming it, but since removing the Admin model seemed not to matter, I suspect a rename would be irrelevant as well.
Also, this issue only occurs on Ubuntu Linux. I tested the actual Rails application itself on Ubuntu, and it seems to not have the same problem as RSpec. Does rake boot the controllers in the same order that Rails and script/runner?
-
Scott Taylor August 12th, 2008 @ 05:12 AM
AFAIK, rails uses const_missing to load (or require) anything which isn't already loaded (or required).
Tony - what version of ruby, rubygems, and rspec are you using on ubuntu? Was the version of ruby installed via source or through apt-get?
-
Tony Perrie August 12th, 2008 @ 05:17 AM
Scott,
Ubuntu Feisty Fawn is running a fully updated Ruby 1.8.5. Our Rails version is 2.1. We saw the same issue on an another Linux box running Ruby 1.8.6 though. So, it seems not to be a 1.8.5 or 1.8.6 issue. The strange thing is, we don't see the problem at all on Ruby 1.8.6 under OS X 10.5.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Behaviour Driven Development for Ruby.
