Asserting that a particular exception is thrown in Cucumber

前端 未结 4 1758
北海茫月
北海茫月 2021-02-02 13:20

Scenario

I\'m writing a library (no Ruby on Rails) for which I\'d like to have very detailed Cucumber features. This especially includes describing erro

4条回答
  •  星月不相逢
    2021-02-02 13:46

    It is possible to raise an exception in a When block and then make assertions about it in the following Then blocks.

    Using your example:

    When /^I do something unwanted$/ do
      @result = -> { throw_an_exception! }
    end
    
    Then /^an "(.*)" should be thrown$/ do |error|
      expect{ @result.call }.to raise_error(error)
    end
    

    That example uses RSpec's matchers but the important part is the -> (Lambda); which allows the reference to the throw_an_exception! method to be passed around.

    I hope that helps!

提交回复
热议问题