假设我们有一个 ruby file.rb 像:
if __FILE__ == $0 then
if ARGV[0] == 'foo'
puts "working"
# Dir.chdir(../)
v = Someclass.new
v.do_something
end
end
working
它假设仅在文件被触发时打印ruby file.rb foo
。
我的问题:如何在 rspec 中测试这种东西?
我的尝试如下。该文件运行但不在 rspec 测试范围内:
Dir expected :chdir with (any args) once, but received it 0 times
it 'should work' do
FILE = File.expand_path('file.rb')
RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
@v = Someclass.new
Someclass.should_receive(:new).and_return @v
@v.should_receive(:do_something)
`#{RUBY} #{FILE} foo`
end