Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top