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?
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
group :custom_test do gem 'gemname', 'x.y.z' end
And then run your tests as in
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.)