Forum: RSpec warning: toplevel constant XYZ referenced Admin:XYZ

E2551c8d439fca5315032f9f3340c668?d=identicon&s=25 dblock (Guest)
on 2011-04-15 22:59
(Received via mailing list)
I have an odd problem. I got controllers in a namespace and
controllers outside of the namespace. For example, I have a
PagesController and a Admin::PagesController.

When I run rspec from the top, tests pass and I get the following
warning:

spec/controllers/admin/pages_controller_spec.rb:4: warning: toplevel
constant PagesController referenced by Admin::PagesController

This makes no sense. I do have a PagesController and an
Admin::PagesController and specs for both that are declared properly.

I get this for 3 out of about 20 controllers.

Any ideas?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2011-04-15 23:59
(Received via mailing list)
On Apr 15, 2011, at 11:02 AM, dblock wrote:

> This makes no sense. I do have a PagesController and an
> Admin::PagesController and specs for both that are declared properly.
>
> I get this for 3 out of about 20 controllers.
>
> Any ideas?

Can you post the top 4 lines of pages_controller_spec?
Ebb5bc7dc252fdde6d74d4716f163e49?d=identicon&s=25 Mike Mazur (Guest)
on 2011-04-16 01:55
(Received via mailing list)
Hi,

On Sat, Apr 16, 2011 at 00:02, dblock <dblockdotorg@gmail.com> wrote:
> This makes no sense. I do have a PagesController and an
> Admin::PagesController and specs for both that are declared properly.

I would guess it's Ruby trying to warn you that the use of the
constant PagesController in Admin::PagesController references the
top-level PagesController, not Admin::PagesController. It can't know
which one you mean.

To make the warning go away, you can use ::PagesController where you
mean the top-level PagesController.

HTH,
Mike
E2551c8d439fca5315032f9f3340c668?d=identicon&s=25 dblock (Guest)
on 2011-04-17 20:37
(Received via mailing list)
> To make the warning go away, you can use ::PagesController where you
> mean the top-level PagesController.

I tried saying ::PagesController in the 2 places it's declared (the
controller itself and the spec), no difference.
E2551c8d439fca5315032f9f3340c668?d=identicon&s=25 dblock (Guest)
on 2011-04-17 20:37
(Received via mailing list)
None of this makes any sense :)

dblock@dblock-magenta:~/source/Gravity/dblock$ find . -name "*.rb" |
xargs grep PagesController

./spec/controllers/admin/pages_controller_spec.rb:describe
Admin::PagesController do
./spec/controllers/pages_controller_spec.rb:describe PagesController
do
./app/controllers/admin/pages_controller.rb:class
Admin::PagesController < AdminController
./app/controllers/pages_controller.rb:class PagesController <
ApplicationController

Now, lets compare with a controller that doesn't yield the same issue.

dblock@dblock-magenta:~/source/Gravity/dblock$ find . -name "*.rb" |
xargs grep ArtworksController
./spec/controllers/admin/artworks_controller_spec.rb:describe
Admin::ArtworksController do
./spec/controllers/artworks_controller_spec.rb:describe
ArtworksController do
./app/controllers/admin/artworks_controller.rb:class
Admin::ArtworksController < AdminController
./app/controllers/artworks_controller.rb:class ArtworksController <
ApplicationController

These are identical, unless I'm blind :) The only thing different is
timestamps - it looks like in the case where things work the Admin
spec is loaded after the non-admin spec.
E2551c8d439fca5315032f9f3340c668?d=identicon&s=25 dblock (Guest)
on 2011-04-17 21:15
(Received via mailing list)
Top 4 lines of pages_controller_spec.

require 'spec_helper'
require 'spec_controllers_helper'

describe PagesController do
  before(:each) do
    controller.stub!(:app_initialization).and_return(true)
  end

Top 4 lines of admin/pages_controller_spec.

require 'spec_helper'
require 'spec_controllers_helper'

describe Admin::PagesController do
  include Devise::TestHelpers
  before(:each) do
    controller.stub!(:app_initialization).and_return(true)
    log_in_test_user(Admin)
  end

(changing PagesController to ::PagesController does nothing)
3d925b45ac07ec0ae5bd04888f6c5b61?d=identicon&s=25 Daniel D. (daniel_d33)
on 2012-01-08 01:13
Someone made some sense out of this.

http://code.dblock.org/warning-toplevel-constant-x...
83b8f908b1aba3c20ef79cce3aeefd58?d=identicon&s=25 Tom D. (tom_d)
on 2013-05-15 16:40
the following is lifted from the 'Gotchas' section of my own internal
wiki, i hope it help's someone else some day.


Can't load namespaced controllers

Errors:

warning: toplevel constant Finance referenced by Member::Finance

OR

uninitialized constant Finance

Cause:

a namespaced controller / helper which is listed BEFORE it's dependency.
e.g. ll member/finance/ bank_batches_controller.rb base_controller.rb

Fix:

make this the first line of the dependent require_dependency
"#{RAILS_ROOT}/app/controllers/member/finance/base_controller".
requiring the dependent file before attempting to load the class. Yes,
this goes before class ...
This topic is locked and can not be replied to.