'warning: toplevel constant XYZ referenced' namespace disambiguation fix fails with spork

232 views
Skip to first unread message

Gallagher Polyn

unread,
Jul 4, 2013, 11:21:20 AM7/4/13
to rs...@googlegroups.com
Hey,

Using spork, rspec-rails and rails 3, I'm getting a 'spec/models/price_request/integer_variable_spec.rb:4: warning: toplevel constant IntegerVariable referenced by PriceRequest::IntegerVariable' warning and corresponding failures in the integer_variable_spec mentioned, due to a models namespace clash. 

Here's a simplified depiction of my rails app models directory (indicates basis for the clash):

models/
       integer_variable
       price_request/
                      integer_variable

... where the classes are named IntegerVariable and PriceRequest::IntegerVariable, respectively.

As discussed at www.ruby-forum.com/topic/1506818, and with the chosen fix given specifically at code.dblock.org/warning-toplevel-constant-xyz-referenced-adminxyz, I added a line to spec_helper to modify the load order in hopes of resolving the problem:

require 'rubygems'
require 'spork'

Spork.prefork do

  ENV["RAILS_ENV"] ||= 'test'
  
  require 'rails/application'
  require 'rails/mongoid'

  Spork.trap_class_method(Rails::Mongoid, :load_models)
  Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
  
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
   
  require File.expand_path("../../config/environment", __FILE__)
  
  Dir[File.expand_path("app/models/price_request/*.rb")].each do |file|
    require file
  end
  ...

This resolves the problem, but spork no longer mirrors the code as it changes, rendering it useless.

Anyone know where to insert the 'fix' line in the spec_helper, so that spork will work properly?

Thanks,

G

Aaron Kromer

unread,
Jul 4, 2013, 9:02:18 PM7/4/13
to rs...@googlegroups.com

Based on your setup, that shouldn't really be an issue. The post you linked to stated, that the root cause was that they had a module and a class with the same name:

It was because I also had a class Admin, as well as a namespace Admin. Since Admin was a class (a model) it inherited from Object which made the top-level ApplicationController available inside the Admin namespace. The reply by Andrew White on http://groups.google.com/group/rubyonrails-core/browse_thread/thread/bab5e87ee10d2ecb lead me to find the right answer. In the end I renamed Admin to AdminUser and everything fell into place.

I think this sort of base conflict is asking for heisenbugs, due to the order that rails just happens to autoload first.

Are you sure that isn't what you have as well?

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/f5e90f8b-a63c-44cd-a78a-f0f22524ed6c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gallagher Polyn

unread,
Jul 8, 2013, 3:45:30 PM7/8/13
to rs...@googlegroups.com

I think this sort of base conflict is asking for heisenbugs, due to the order that rails just happens to autoload first.

By 'heisenbugs' you mean something I could avoid in test by changing the model file structure?

With a little more snooping, I found a fix suggested here, which I applied to rectify my failing spec test file as follows:

require 'spec_helper'
require_dependency './app/models/price_request/integer_variable' # NEW 'FIX' LINE

describe PriceRequest::IntegerVariable do
          ... 

I'd prefer the fix in the spec_helper, but the above works.

Thanks,

G-man 

Aaron Kromer

unread,
Jul 8, 2013, 4:48:36 PM7/8/13
to rs...@googlegroups.com

What I mean is, it seems you are very much relying fixing the spec by messing with the load order in a non-standard way. This means if/when the load order changes (say in the rails app since rails lazy loads), you will have issues. Why are you not just using the standard require or require_relative to make sure these types of dependencies are pre-loaded?

# price_request.rb
module PriceRequest
  # code
end

# price_request/integer_variable.rb
require_relative '../price_request'

module PriceRequest
  class IntegerVariable
    # code
  end
end

# spec/price_request/integer_variable_spec.rb
require 'spec_helper'
require 'price_request/integer_variable'

describe PriceRequest::IntegerVariable do

  # specs
end


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages