mockk 异常 - 找不到答案

Par*_*war 9 kotlin mockk

使用 mockk 测试 kotlin 功能。

private val serviceObject = mockk<Service>()
private val serviceToBeTested = ServiceToBeTestd(Service)
        
fun test(){
    when(serviceObject.function1(argument1,argument1))
        .thenReturn(<something>)
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,出现此错误:

io.mockk.MockKException: no answer found for: Service(#1).function1(argument1, argument2)
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?

ServiceToBeTestd 是要测试的服务,Service 连接在其中:

open class ServiceToBeTestd
    constructor(private val service: Service)
Run Code Online (Sandbox Code Playgroud)

Har*_*nia 13

您正在使用模拟语法。

以下是 mockk 的正确语法。

val car = mockk<Car>()

every { car.drive(Direction.NORTH) } returns Outcome.OK
Run Code Online (Sandbox Code Playgroud)

请更新您的语法。