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
精彩评论