Ruby RSpec test for success or failure based on gem version being used

Is there a way to write an Rspec test to prove that some code fails if we are using a specific version of a gem and passes if we use another version of the same gem?
What I do currently is have one version in my Gemfile and then run rspec spec and see a test pass. Then I modify my Gemfile with the different gem version and bundle update and then run rspec spec again and see the same test fail.
I would like to have two tests, one that loads one version of the gem and tests for normal execution and succeeds and another test that loads a different version of the gem and tests for an exception and succeeds on the raised exception and both tests are run on the same rspec spec run. Is this even possible. If not, any suggestions on anything that does not involve me having to modify my Gemfile manually?

Also, in case it helps
I currently use bundler to install gems.
ruby 1.9.3p545
rspec 2.14.1
Also, I am not using Rails

  • Ruby Gemspec Dependency: Is possible have a git branch dependency?
  • rvm-capistrano errors on ruby 2.2.3
  • Local gem installed by Bundler is removed automatically
  • requiring a custom gem
  • do i have to run ruby gem update even if i use bundler
  • ffi gem unable to build native extension on Cygwin
  • Can gems be assigned different interpreters?
  • Rails port of testing environment
  • Automatic identification of Ruby Gems no longer in use within Rails
  • Spork and cache_classes problem with rspec, factory_girl and datamapper
  • Why won't bundle install my local gem w/ extension?
  • shoulda matchers should validate_uniqueness_of failing with scope
  • 2 Solutions collect form web for “Ruby RSpec test for success or failure based on gem version being used”

    I think you can specify the gem and version you want to use in the code:

    it "does something" do
      gem "gemname", "4.0"
      require "gemname"
      expect(subject).to do_something
    end
    

    But I don’t know if what you’re trying to do is a good idea because you should be testing with the same gems you would be using in production.

    You could define a different environment to include the alternative version of your gem and use this while testing. For example, in your Gemfile

    group :custom_test do
      gem 'gemname', 'x.y.z' 
    end
    

    And then run your tests as in

    RAILS_ENV=custom_test rspec
    

    I strongly suggest however that you look into solutions that let you test against different environments, gemfiles, rails and ruby versions (e.g. Jenkins or Travis etc.)

    Ruby is the best programming language in the world - Ruby on Rails.