开发者

RSpec tests pass individually but fail as suite because of ARGV

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

The problem: ARGV is being set to ["spec"] and running my 开发者_如何学JAVAprogram 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

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

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

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

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

Possibly relevant background: I'm running under rbenv.


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

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

def test_play_jukebox
  parse_options(["play_jukebox"])
end

and in your bin file, have

if $0 == __FILE__
  parse_options(ARGV)
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜