由于 ARGV,RSpec 测试单独通过但作为套件失败 [英] RSpec tests pass individually but fail as suite because of ARGV

查看:35
本文介绍了由于 ARGV,RSpec 测试单独通过但作为套件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个 RSpec2 测试文件,每个文件都单独通过.但是使用 rspec spec(或 jruby -S rspec spec)运行套件失败.

I have three RSpec2 test files, each of which passes individually. But running the suite with rspec spec (or jruby -S rspec spec) fails.

问题:ARGV 被设置为 ["spec"] 并且使用 spec 参数运行我的程序会改变它的行为.我尝试在我的测试中处理这个问题:

The problem: ARGV is being set to ["spec"] and running my program with a spec argument changes its behavior. I try to handle this in my tests with:

before(:each) do
  ARGV.clear  # also tried: ARGV.delete_if { |val| true }
end

但是 puts ARGV 语句在我的代码中表明 ARGV 仍然被设置为 ["spec"].

but the puts ARGV statement in my code indicates ARGV is still being set to ["spec"].

我还创建了一个 spec/spec_helper.rb 文件:

I've also created a spec/spec_helper.rb file with:

RSpec.configure do |config|
  config.before(:suite) do
    ARGV.clear
  end
end

结果相同.当我单独运行测试时,ARGV 为空.但是当我运行 rspec spec 时,ARGV["spec"].

with the same result. When I run tests individually, ARGV is empty. But when I run rspec spec, ARGV is ["spec"].

可能相关的背景:我在 rbenv 下运行.

Possibly relevant background: I'm running under rbenv.

推荐答案

重写您的代码,以便在您测试的方法中不提及 ARGV.

Rewrite your code such that ARGV isn't mentioned in the methods you're testing.

例如,如果你需要测试你可以解析play_jukebox",那么做

For example, if you need to test that you can parse "play_jukebox", then do

def test_play_jukebox
  parse_options(["play_jukebox"])
end

在你的 bin 文件中,有

and in your bin file, have

if $0 == __FILE__
  parse_options(ARGV)
end

这篇关于由于 ARGV,RSpec 测试单独通过但作为套件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆