Mys*_*fle 3 mocking kotlin mockk
我有以下代码(在 Kotlin 中):
class X {
fun foo() {
val A(1, true, "three")
val b = B()
b.bar(A)
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是找出A已实例化的内容。
我的测试代码如下所示:
// Needed for something else
every { anyConstructed<A>().go() } returns "testString"
// What I'm using to extract A
val barSlot = slot<A>()
verify { anyConstructed<B>().bar(capture(barSlot)) }
val a = barSlot.captured
Run Code Online (Sandbox Code Playgroud)
A现在我已经成功捕获了构造时创建的模拟(感谢该语句) ,如何检查已实例化哪些值every?
谢谢!
您可以通过两种方式完成此操作:
用于slot捕获参数:
@Test
fun shouldCheckValuesAtConstruct() {
val a = A(1, true, "s")
val b = mockk<B>()
val aSlot = slot<A>()
every { b.bar(a = capture(aSlot)) } returns Unit
b.bar(a)
val captured = aSlot.captured
assertEquals(1, captured.a)
assertEquals(true, captured.b)
assertEquals("s", captured.s)
}
Run Code Online (Sandbox Code Playgroud)
或者使用withArg函数和内联断言
@Test
fun shouldCheckValuesAtConstructInlineAssertion() {
val a = A(1, true, "s")
val b = mockk<B>()
every { b.bar(a) } returns Unit
b.bar(a)
verify {
b.bar(withArg {
assertEquals(1, it.a)
assertEquals(true, it.b)
assertEquals("s", it.s)
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4515 次 |
| 最近记录: |