[英]How can I check the constructur arguments of a mockk Mock?
我有以下代碼(在 Kotlin 中):
class X {
fun foo() {
val A(1, true, "three")
val b = B()
b.bar(A)
}
}
我想要的是找出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
我如何檢查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)
}
或使用withArg
function 和內聯斷言
@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)
})
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.