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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:58:38+00:00 2026-06-11T08:58:38+00:00

I have a rspec test to validate a function which works depending on the

  • 0

I have a rspec test to validate a function which works depending on the rails version. So in my code I’m planning to use Rails::VERSION::String to get the rails version.

Before the test I tried to explicitly set the rails version like this

Rails::VERSION = "2.x.x"

But when I run the test seems like rspec cannot find the Rails variable and gives me the error

uninitialized constant Rails (NameError)

So what might be I’m missing here, thanks in advance

  • 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-06-11T08:58:39+00:00Added an answer on June 11, 2026 at 8:58 am

    The best way to do this is to encapsulate the rails version check in code that you control, and then stub out the different test values you want to exercise.

    For example:

    module MyClass
      def self.rails_compatibility
        Rails.version == '2.3' ? 'old_way' : 'new_way'
      end
    end
    
    describe OtherClass do
      context 'with old_way' do
        before { MyClass.stubs(:rails_compatibility => 'old_way') }
        it 'should do this' do
          # expectations...
        end
      end
    
      context 'with new_way' do
        before { MyClass.stubs(:rails_compatibility => 'new_way') }
        it 'should do this' do
          # expectations...
        end
      end
    end
    

    Alternately, if your versioning logic is that complex, you should stub out a simple wrapper:

    module MyClass
      def self.rails_version
        ENV['RAILS_VERSION']
      end
    
      def self.behavior_mode
        rails_version == '2.3' ? 'old_way' : 'new_way'
      end
    end
    
    describe MyClass do
      context 'Rails 2.3' do
        before { MyClass.stubs(:rails_version => '2.3') }
        it 'should use the old way' do
          MyClass.behavior_mode.should == 'old_way'
        end
      end
    
      context 'Rails 3.1' do
        before { MyClass.stubs(:rails_version => '3.1') }
        it 'should use the new way' do
          MyClass.behavior_mode.should == 'new_way'
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a failing rspec view test but the code works - I probably
I have problems to get rspec running properly to test validates_inclusion_of my migration looks
I am new to RSpec and I have a test scenario in which I
In our rspec test for rails 3.1.0 app, we use both Factory.build and Factory.attributes_for.
I have a RSpec test for a class in /lib/classes which needs access to
I have an Rspec test for a helper method which requires access to my
My rspec title test is failing with: 1) HomeController Get 'index should have the
I have just started using RSpec and I copied the very simple test on
I am trying to build a test suite using Rspec and I have always
I have the following RSpec example which is passing: describe UsersController do it should

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.