【发布时间】:2019-12-22 07:01:01
【问题描述】:
所以我有以下代码:
When("SMS with location update command is received") {
every {
context.getString(R.string.location_sms, any(), any(), any(), any())
} returns "loc"
mainServiceViewModel.handleSms(SmsMessage("123", "location"))
Then("SMS with location is sent to specified phone number") {
verify(exactly = 1) {
smsRepository.sendSms("+123", "loc")
}
}
}
When("motion is detected") {
Then("information SMS is sent to specified phone number") {
verify(exactly = 1) {
smsRepository.sendSms("+123", any())
}
}
}
它的问题是,这两种情况都通过了,即使第二种情况没有采取任何行动。我预计第二种情况会失败,因为 sendSms 方法甚至没有被调用。
- 如何重置 smsRepository 验证计数?
- 如何在每个 When 情况之前重置该计数?
【问题讨论】:
-
clear方法应该可以帮助您重置计数。wasNot Called也可能对您有所帮助。至于你的第二个问题,哪个框架提供了when和then? -
我的测试扩展的 Kotlintest BehaviorSpec 为 BDD 提供了具有 Given、When、Then 的工具。
标签: unit-testing kotlin bdd mockk kotlintest