有人可以帮助我弄清楚如何有效地对以下代码进行单元测试。它的kotlin,但可以是任何东西:
version: "3.7"
services:
kms:
image: kurento/kurento-media-server:6.9.0
container_name: kms
restart: always
network_mode: host
environment:
- GST_DEBUG=2,Kurento*:5
我想测试doSomethingSpecial确实有效。
我考虑过尝试模拟lambda:
myMainObject.doSomethingSpecial({ differentObject ->
differentObject.doFirst()
differentObject.doSecond()
differentObject.doThird()
})
但这不是我真正想要的。我想测试lamda中的所有方法都被调用,所以我想测试 doFirst,doSecond,doThird 等,我认为这是最好的单元测试方法。我要为此使用参数捕获吗?明确地说,我的问题是如何对方法doSomethingSpecial进行单元测试?
答案 0 :(得分:1)
您可以尝试将问题分为两个简单的问题:
doSomethingSpecial
(我想称它为接收参数的函数)并对其进行测试differentObject
的 mock 并测试 mock 的方法被称为所以,用伪代码:
val myFunction = { differentObject ->
differentObject.doFirst()
differentObject.doSecond()
differentObject.doThird()
differentObject.doFourth()
differentObject.doFifth()
}
myMainObject.doSomethingSpecial(myFunction)
// Invoke doSomethingSpecial with fake parameter
val fakeFunction = ...
myMainObject.doSomethingSpecial(fakeFunction)
// Assert fakeFunction was called
// Invoke myFunction with mock parameter
val mockObject = ...
myFunction(mockObject)
// Assert mockObject.doNth was called
我希望这种方法可以帮助您找到真正的解决方案!