gpt4 book ai didi

unit-testing - MockK "io.mockk.MockKException: no answer found for:"错误

转载 作者:行者123 更新时间:2023-12-04 04:19:30 26 4
gpt4 key购买 nike

嗨,我试图模拟我从使用我的演示者类调用的委托(delegate)器从改造返回的 Single observable 获得的响应,并且我收到以下错误:

io.mockk.MockKException: no answer found for: LoginPresenter(#1).login(LoginRequest(email=hello@gmail.com, password=password123))


这是我的测试代码
@Test
fun testKotlinMock(){

val presenter : LoginPresenter = mockk<LoginPresenter>()

val delegator = mockk<AccountDelegatorContract>()

val viewCallback = mockk<LoginContract.LoginViewCallBack>()

val cookieStore = mockk<PianoCookieStore>()

val loginRequest = LoginRequest("hello@gmail.com", "password123")
val customerResponse = CustomerResponse("jon", "richy")

every { delegator.login(loginRequest) } returns Single.just(Response.success(any()))
every { delegator.getCustomer() } returns Single.just(customerResponse)
every { presenter.loginViewCallBack } returns viewCallback
every { presenter.accountDelegator } returns delegator
every { presenter.cookieStorage } returns cookieStore

presenter.login(loginRequest)
}
我实际的 Presenter 代码如下所示:
@Inject
lateinit var loginViewCallBack: LoginViewCallBack

@Inject
lateinit var delegator: DelegatorContract

@Inject
lateinit var cookieStorage: CookieStore

@Inject
constructor()

override fun login(loginRequest: LoginRequest) {
delegator.login(loginRequest)
.flatMap({ response ->
saveCookieAndContinue(response)
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(object : SingleObserver<CustomerResponse>{
override fun onSubscribe(d: Disposable) {
}

override fun onError(e: Throwable) {
loginViewCallBack.onErrorLogin(PianoError.ERROR_LOGIN_INVALID)
Log.d("JJJ", "login error")
}

override fun onSuccess(customerResponse : CustomerResponse) {
loginViewCallBack.onLoginSuccess(customerResponse)
Log.d("JJJ", "login successfully")
}
})
}

private fun saveCookieAndContinue(response: Response<Void>): Single<CustomerResponse> {
if (response.isSuccessful) {
val headers = response.headers()
cookieStorage.saveSessionCookies(headers.get(PianoCookieStore.COOKIE_HEADER_SET_NAME)!!)
return accountDelegator.getCustomer()
}
//TODO: Change this to throw a login exception?
throw RuntimeException()
}
我基本上想模拟您从主代码中看到的注入(inject)依赖项,然后运行快乐路径单元测试。
当我调用 presenter.login(loginRequest) 并没有找到答案错误时,它失败了
这是我正在使用的 kotlin 扩展插件 http://mockk.io/

最佳答案

就我而言,我忘记检查是否调用了一种方法(例如,在代码中我们有 object.setData(person.getAge()) )。

every { object.setData(any()) } just Runs // Mock method
testedMethod() // Tested code runs here
verify { object.setData(any()) } // Check that object.setData() was called

关于unit-testing - MockK "io.mockk.MockKException: no answer found for:"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com