How can I validate exits and aborts in RSpec?

前端 未结 7 892
傲寒
傲寒 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:17

    I had to update the solution @Greg provided due to newer syntax requirements.

    RSpec::Matchers.define :exit_with_code do |exp_code|
      actual = nil
      match do |block|
        begin
          block.call
        rescue SystemExit => e
          actual = e.status
        end
        actual and actual == exp_code
      end
      failure_message do |block|
        "expected block to call exit(#{exp_code}) but exit" +
            (actual.nil? ? " not called" : "(#{actual}) was called")
      end
      failure_message_when_negated do |block|
        "expected block not to call exit(#{exp_code})"
      end
      description do
        "expect block to call exit(#{exp_code})"
      end
      supports_block_expectations
    end
    

提交回复
热议问题