本文介绍了如何在 RSpec 中验证退出和中止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为脚本接收的命令行参数指定行为,以确保所有验证都通过.我的一些命令行参数将导致 abortexit 被调用,因为提供的参数丢失或不正确.

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:exit").

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 中验证退出和中止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:15