PS:代码将在 Koltin
例如,我有我的服务类,它做一些事情并注入一些其他服务。
class MyService(
private val someOtherService: OtherService
) {
fun doSomething() {
someOtherService.someMethod("foo")
someOtherService.someMethod("bar")
someOtherService.someMethod("baz")
}
}
这是我对模拟 OtherService 的 MyService 类的测试:
internal class MyServiceTest {
@MockkBean(relaxed = true)
private lateinit var someOtherService: OtherService
@Test
fun `my test description`() {
every { someOtherService.someMethod(any()) } just Runs
verify(exactly = 1) {
someOtherService.someMethod(
match {
it shouldBe "bar"
true
}
)
}
}
结果,"bar"
参数将是预期的,但将是"foo"
参数,并且测试将失败。
原因:someOtherService.someMethod("foo")
之前会打电话someOtherService.someMethod("bar")
。
但是,我想验证每个方法都只调用一次。我怎么能这样做?