How can I validate exits and aborts in RSpec?

前端 未结 7 859
傲寒
傲寒 2020-12-04 23:57

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 abo

相关标签:
7条回答
  • 2020-12-05 00:28

    After digging, I found this.

    My solution ended up looking like this:

    # something.rb
    class Something
        def initialize(kernel=Kernel)
            @kernel = kernel
        end
    
        def process_arguments(args)
            @kernel.exit
        end
    end
    
    # something_spec.rb
    require 'something'
    describe Something do
        before :each do
            @mock_kernel = mock(Kernel)
            @mock_kernel.stub!(:exit)
        end
    
        it "should exit cleanly" do
            s = Something.new(@mock_kernel)
            @mock_kernel.should_receive(:exit)
            s.process_arguments(["-h"])
        end
    end
    
    0 讨论(0)
提交回复
热议问题