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 6706017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:28:15+00:00 2026-05-26T07:28:15+00:00

I have a gem, which has a method which acts differently depending on the

  • 0

I have a gem, which has a method which acts differently depending on the Rails.env:

def self.env
  if defined?(Rails)
    Rails.env
  elsif ...

And now I’d like to write a spec, which tests this code path. Currently I’m doing it like this:

Kernel.const_set(:Rails, nil)
Rails.should_receive(:env).and_return('production')
...

And it’s ok, just feels ugly. Another way is to declare this in spec_helper:

module Rails; end

And it works as well. But maybe there is a better way? Ideally this should work:

rails = double('Rails')
rails.should_receive(:env).and_return('production')

But, well, it does not. Or maybe I’m doing something wrong?

  • 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-26T07:28:15+00:00Added an answer on May 26, 2026 at 7:28 am

    Per the various tweets about this, switching on constants is generally a bad idea because it makes things a bit of a challenge to test and you have to change the state of constants in order to do so (which makes them a little less than constant). That said, if you’re writing a plugin that has to behave differently depending on the environment in which it’s loaded, you’re going to have to test on the existence of Rails, Merb, etc to somewhere, even if it’s not in this particular part of the code. Wherever it is, you want to keep it isolated so that decision happens only once. Something like MyPlugin::env. Now you can safely stub that method in most places, and then spec that method by stubbing constants.

    As to how to stub the constants, your example doesn’t look quite right. The code is asking if defined?(Rails), but Kernel.const_set(:Rails, nil) doesn’t undefine the constant, it just sets its value to nil. What you want is something like this (disclaimer – this is off the top of my head, untested, not even run, may contain syntax errors, and is not well factored):

    def without_const(const)
      if Object.const_defined?(const)
        begin
          @const = const
          Object.send(:remove_const, const)
          yield
        ensure
          Object.const_set(const, @const)
        end
      else
        yield
      end
    end
    
    def with_stub_const(const, value)
      if Object.const_defined?(const)
        begin
          @const = const
          Object.const_set(const, value)
          yield
        ensure
          Object.const_set(const, @const)
        end
      else
        begin
          Object.const_set(const, value)
          yield
        ensure
          Object.send(:remove_const, const)
        end
      end
    end
    
    describe "..." do
      it "does x if Rails is defined" do
        rails = double('Rails', :env => {:stuff_i => 'need'})
        with_stub_const(:Rails, rails) do
          # ...
        end
      end
    
      it "does y if Rails is not defined" do
        without_const(:Rails) do
          # ....
        end
      end
    end
    

    I’ll give some thought as to whether we should include this in rspec or not. It’s one of those things that if we added people would use it as an excuse to rely on constants when they don’t need to 🙂

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

Sidebar

Related Questions

I have a Rails 3 gem which has some rake tasks that should only
I have a engine style Rails plugin from which I can create a gem
I have a RAILS 3.x application developed locally using MySQL which has data which
I have an application which has the requirement, but infrequent usage, of uploading an
I have a gem, roundhouse , which is an application compiled with .NET (C#).
Say I have written a gem which makes lazy calls to the Google Distance
I have a item tracking system which has an Item model which has a
I'm creating a gem which has several scripts in the bin directory the utility
I have developed a gem which implements some algorithms that we'll use within a
I need switching gem on which other gem relies. So in particular: i have

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.