【问题标题】:Jbehave and expecting exceptionsJbehave 和期待异常
【发布时间】:2009-09-25 18:28:37
【问题描述】:

我在 JBehave 中有一个“何时”,在某些情况下应该抛出异常。但是,我找不到任何有关如何处理此问题的文档。这是我的场景:

给定一个有 6 个现有赌注的游戏,该游戏的最大赌注为 6 当用户下注时

那就没有了,因为我想在用户下注时抛出异常。

请记住,我并不总是希望何时抛出异常。例如。当现有赌注小于最大赌注时。在那种情况下,我想在“then”中做一些确保。

【问题讨论】:

    标签: jbehave


    【解决方案1】:

    实施细节和“计算机语言”不属于场景


    我对 BDD 的理解是它以利益相关者为中心,允许非技术人员使用(或多或少)自然的方式编写系统应该做的事情语言,以开发人员可以编码的方式:

    它通过编写测试用例来扩展 TDD:

    用一种自然语言 非程序员可以阅读。
    [wikipedia]

    也就是说,利益相关者永远不会在“那么”中写“抛出异常”。他们可能会写:

    Given a game where 6 bets are allowed and 5 bets have been made,
    When a user makes the 6th bet,
    Then the "Bet" button should become disabled.
    

    Given a game where 6 bets are allowed and 6 bets have been made,
    When a user tries to make a bet,
    Then the a message appears, saying: 
           "You can not bet. The maximum of 6 bets has already been placed."
    

    【讨论】:

      【解决方案2】:

      由于还没有答案,我会试一试。我这样做的方法是将异常存储为 Steps 实现的内部状态的一部分:

      public class MySteps extends Steps {
      
        private Throwable throwable = null;
      
        @When("something happens")
        public void somethingHappens() {
          try {
            // Event part of the specification          
          } catch (MyException e) {
            throwable = e;
          }
        }
      
        @Then("an exception is thrown") {
        public void verifyException() {
          assertThat(throwable, allOf(notNullValue(), myExceptionMatcher())); 
        }
      
        private Matcher<Throwable> myExceptionMatcher() {
          // Build and return some Matcher instance
        }
      
      }
      

      这对我来说效果很好,但需要仔细的状态管理。

      【讨论】:

        【解决方案3】:

        我们使用了 Nils Wloka 建议的模式,非常有效的答案,使用 try --> catch 捕获“When”中的异常,然后在“Then”步骤中针对预期的异常进行验证。

        【讨论】:

          猜你喜欢
          • 2012-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-22
          • 1970-01-01
          • 2020-01-25
          • 2023-03-07
          • 2017-07-26
          相关资源
          最近更新 更多