Ruby RSpec根据使用的gem版本测试成功或失败

时间:2021-11-19 00:21:45

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?

有没有办法编写Rspec测试来证明如果我们使用特定版本的gem而某些代码会失败,如果我们使用同一个gem的另一个版本就会通过?我目前所做的是在我的Gemfile中有一个版本,然后运行rspec规范并查看测试通过。然后我用不同的gem版本和bundle更新修改我的Gemfile,然后再次运行rspec spec并看到相同的测试失败。我想有两个测试,一个加载一个版本的gem并测试正常执行和成功,另一个测试加载不同版本的gem并测试异常并成功引发异常并且两个测试都运行在相同的rspec规范运行。这是否可能。如果没有,对任何不涉及我手动修改我的Gemfile的任何建议?

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

此外,如果它有助于我目前使用bundler安装宝石。 ruby 1.9.3p545 rspec 2.14.1另外,我没有使用Rails

2 个解决方案

#1


0  

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

我想你可以在代码中指定你想要使用的gem和版本:

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.

但是我不知道你要做的是不是一个好主意,因为你应该用你在生产中使用的相同宝石进行测试。

#2


0  

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

您可以定义不同的环境以包含gem的替代版本,并在测试时使用它。例如,在你的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.)

不过我强烈建议你研究解决方案,让你测试不同的环境,gemfiles,rails和ruby版本(例如Jenkins或Travis等)

#1


0  

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

我想你可以在代码中指定你想要使用的gem和版本:

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.

但是我不知道你要做的是不是一个好主意,因为你应该用你在生产中使用的相同宝石进行测试。

#2


0  

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

您可以定义不同的环境以包含gem的替代版本,并在测试时使用它。例如,在你的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.)

不过我强烈建议你研究解决方案,让你测试不同的环境,gemfiles,rails和ruby版本(例如Jenkins或Travis等)