Asserting that a particular exception is thrown in Cucumber

前端 未结 4 1757
北海茫月
北海茫月 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:55

    One option is to mark the scenario with @allow-rescue and check the page's output and status code. For example

    In my_steps.rb

    Then(/^the page (?:should have|has) content (.+)$/) do |content|
      expect(page).to have_content(content)
    end
    
    Then(/^the page should have status code (\d+)$/) do |status_code|
      expect(page.status_code.to_s).to eq(status_code)
    end
    
    Then /^I should see an error$/ do
      expect(400..599).to include(page.status_code)
    end
    

    In my_feature.feature

    @allow-rescue
    Scenario: Make sure user can't do XYZ
      Given some prerequisite
      When I do something unwanted
      Then the page should have content Routing Error
      And the page should have status code 404
    

    or alternatively:

    @allow-rescue
    Scenario: Make sure user can't do XYZ
      Given some prerequisite
      When I do something unwanted
      Then I should see an error
    

    This may not be exactly what you were hoping for, but it might be an acceptable workaround for some people who come across this page. I think it will depend on the type of exception, since if the exception is not rescued at any level then the scenario will still fail. I have used this approach mostly for routing errors so far, which has worked fine.

提交回复
热议问题