如何验证 RSpec 中的退出和中止? [英] How can I validate exits and aborts in RSpec?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆