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

  • How can Bundler/Gemfile be configured to use different gem sources during development?
  • Bypassing bundler for auxiliary development gems
  • Specify Compass/Sass version for grunt build
  • Switching over to zsh breaks rails in command line
  • Local gem installed by Bundler is removed automatically
  • How do I freeze gems into a Rails 3 application?
  • Bundler could not find compatible versions for gem “rubyzip”
  • Installing a gem blocked?
  • How can I disable MySQL unique constraints in a RSpec test
  • How to vendor a modified version of active_support such that it is used in my Rails app?
  • Bundler installing gem that's already installed
  • Avoid “bundle install” to use pre-compiled gems
  • 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

    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' 

    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.