Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:24:58+00:00 2026-05-31T13:24:58+00:00

After watching the Fasts Rails Tests talk by Corey and reading through Object on

  • 0

After watching the Fasts Rails Tests talk by Corey and reading through Object on Rails by Avdi I am in the process of setting up my new Rails 3.2 application to take advantage of these concepts.

In order to get everything working I started with this example spec file.

# spec_no_rails/projects/financials_spec.rb
require_relative '../spec_no_rails_helper'

class DummyProject
  include Modules::Projects::Financials
end

describe Modules::Projects::Financials do
  it 'should have some method' do
    DummyProject.new.foo.should == 'bar'
  end
end

And this was the intialial spec_no_rails_helper.rb file that is used to require the modules

# spec_no_rails/spec_no_rails_helper.rb
Dir["#{Dir.pwd}/app/pimms/**/*.rb"].each { |file| require file }

I then set about creating the new example module.

# app/pimms/modules/projects/financials.rb
module Modules::Projects::Financials
  def foo
    'bar'
  end
end

In order to see that everything was going to work when I included the new stand alone module into one of my ActiveRecord classes I added the following line into one of my models.

# app/models/project.rb
class Project < ActiveRecord::Base
  include Modules::Projects::Financials
end

This allowed me to open up console and see that everything is working as expected.

> Project.first.foo
=> "bar"

So at this stage I have defined a namespaced stand alone module defined under app/pimms/modules/projects/financials.rb which I can included into a Rails model and everything works as expected.

The problem I’m having is when I try to run the specs I get the following.

> bundle exec rspec spec_no_rails/
/Users/scott/Code/pimms/spec_no_rails/projects/financials_spec.rb:5:in `<class:DummyProject>': uninitialized constant DummyProject::Modules (NameError)
  from /Users/scott/Code/pimms/spec_no_rails/projects/financials_spec.rb:4:in `<top (required)>'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
  from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'

So the reason this happens is because the Modules::Projects namespaces has not been defined when the tests are being run. I’m guesing I didn’t have to define the Modules::Projects namespace when I used the module with the Rails application because Rails handled that for me.

In order to get the test to run as expected I had to define the namespace in the spec_no_rails_helper.rb file like this.

# spec_no_rails/spec_no_rails_helper.rb
module Modules
  module Projects
  end
end

Dir["#{Dir.pwd}/app/pimms/**/*.rb"].each { |file| require file }

This obviously isn’t ideal as I would have to manually create all the namespaces for any stand alone modules or classes that I wasnt to test outside of Rails.

Is there a better way to setup my Rails application so that I can easily run a test suite without relying on Rails?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:24:59+00:00Added an answer on May 31, 2026 at 1:24 pm

    I think this is because of how you’ve defined the module. You’re attempting to define it like this:

    module Modules::Projects::Financials
      def foo
       'bar'
      end
    end
    

    But Ruby will interpret this by attempting to find the Modules module first, then the Projects module and then will finally define the Financials module, but ONLY if it can find the first two.

    In your case, it can’t find the first two and so bombs out like that.

    Go ahead, fire up irb and copy and paste the above code example into it. You’ll see exactly the same error that you’re getting when you run your specs.


    The way to fix this is to just define/re-open the module in each file:

    module Modules
      module Projects
        module Financials
          def foo
            'bar'
          end
        end
      end
    end
    

    Now that each time each one of these files is loaded, it will either define or re-open the modules, adding functionality to them. The best part about this is that it doesn’t matter if Modules is or isn’t defined first, it’ll just define it anyway.


    Now to address the point: Why does it work in Rails?

    Oh man, Rails is totally doing some awesome stuff!

    I actually covered how Rails deals with the automatic definition of modules in my “wrong argument type” screencast. Well, I don’t exactly cover how modules are automatically loaded, but (spoiler alert) it’s the culprit for what’s going wrong there.

    I’m not going to force you to watch it. The problem lies with these lines inside activesupport/lib/active_support/dependencies.rb.

    The load_missing_constant method is used by Rails when it can’t find a constant. That’s when Rails’s magic autoloading stuff kicks into gear. It calls this method, and attempts to find a file that defines this module.

    If it can’t do that, it does this:

    mod = Module.new
    into.const_set const_name, mod
    autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
    return mod
    

    This is inside the autoload_module! method in that dependencies.rb file.

    What this code does is simple: creates a new module, sets its constant name to the one that’s missing, adds the qualified_name to the autoloaded_constants and returns that module.

    This is why Rails is defining your modules even though they don’t really exist. You’re completely bypassing this in your spec (with good reason, you don’t want all of that nasty Rails junk), and so it’s not automatically loading the modules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inspired after watching Michael Feather's SCNA talk Self-Education and the Craftsman , I am
I'm currently working on a little text-based game after watching the new Tron movie
Well, after one week of watching and reading tutorials I still couldn't manage to
I’m debugging a Java application that runs several threads. After a while of watching
After watching Guido's Google IO talk and hearing the mention that it can be
I made my own REST client library for an Android application, but after watching
After reading ASP.NET MVC 2 in Action and watching Jimmy Bogard's presentation from MvcConf
After watching The Dark Knight I became rather enthralled with the concept of the
After watching: The Clean Code Talks -- Inheritance, Polymorphism, & Testing I checked my
After watching the webinar, skimming over the BuckyBook PDF, and following the Eclipse RCP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.