问题描述
我正在尝试为脚本接收的命令行参数指定行为,以确保所有验证均通过.我的某些命令行参数将导致abort
或exit
被调用,因为提供的参数丢失或不正确.
I am trying to spec behaviors for command line arguments my script receives to ensure that all validation passes. Some of my command line arguments will result in abort
or exit
being invoked because the parameters supplied are missing or incorrect.
我正在尝试这种不起作用的事情:
I am trying something like this which isn't working:
# something_spec.rb
require 'something'
describe Something do
before do
Kernel.stub!(:exit)
end
it "should exit cleanly when -h is used" do
s = Something.new
Kernel.should_receive(:exit)
s.process_arguments(["-h"])
end
end
exit
方法正在彻底触发,阻止RSpec验证测试(我得到"SystemExit:退出").
The exit
method is firing cleanly preventing RSpec from validating the test (I get "SystemExit: exit").
我也尝试过mock(Kernel)
,但是也无法按我的意愿进行(我看不到任何明显的区别,但这很可能是因为我不确定如何精确模拟内核并确保我的Something
类中使用了模拟内核).
I have also tried to mock(Kernel)
but that too is not working as I'd like (I don't see any discernible difference, but that's likely because I'm not sure how exactly to mock Kernel and make sure the mocked Kernel is used in my Something
class).
推荐答案
尝试一下:
module MyGem
describe "CLI" do
context "execute" do
it "should exit cleanly when -h is used" do
argv=["-h"]
out = StringIO.new
lambda { ::MyGem::CLI.execute( out, argv) }.should raise_error SystemExit
end
end
end
end
这篇关于如何验证RSpec中的退出和中止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!