题
我有一个“时”,在其中JBehave在某些情况下应抛出异常。我无法找到任何文件,但是,对如何处理这个问题。这里是我的情况:
给定的一个游戏6个现有赌注和游戏的最大投注6 当用户将下注
没有的话,因为我想抛出一个异常时,用户将赌注。
请记住,我并不总是想什么时候抛出异常。例如。当现有的赌注都小于最大赌注。在这种情况下,我想要做的“然后”一些保证。
解决方案
实施细则和“计算机ESE”不要在方案不属于
,点击 我BDD的理解是,它的的利益相关者的-focused,允许的非技术的人写什么系统是应该做的,使用(更多或更少)的天然语言,在某种程度上,开发人员可以编写为:
它通过写测试用例延伸TDD: 的
的在自然语言 非程序员可以读取。的 结果,[维基] 搜索结果
这是说,一个利益相关者永远不会写的“那”,“抛出一个异常”。他们可能会写:
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."
其他提示
由于没有解答,我会试试看。我做到这一点的方法是通过存储例外作为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
}
}
这很适合我,但需要仔细状态管理。
我们已经使用尼尔斯Wloka,非常有效的回答提出的模式,“当”使用try捕获例外 - >捕获,然后验证对预期的例外“然后”一步
不隶属于 StackOverflow