编辑:对于未来的读者。不知道这个问题对你有没有很大的帮助。乐趣的逻辑已经发生了巨大的变化,所以我关闭了这个问题,但不会删除它。
我正在尝试为我的 ViewModel 编写一些单元测试。我正在使用 Mockk 和 Junit5。
应该发生什么:模拟存储库返回 fakeresponse,我在 VM 中调用 fun,它将 livedata 设置为伪响应数据。
实际发生的情况:
Exception in thread "DefaultDispatcher-worker-2 @coroutine#1" io.mockk.MockKException: no answer found for: DrillRepository(#1).loadDrillTypes()
at io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:90)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at com.nikolam.basketpro.data.DrillRepository.loadDrillTypes(DrillRepository.kt:11)
at com.nikolam.basketpro.ui.drills.selection.DrillsSelectionViewModel$fetchDrillTypes$1.invokeSuspend(DrillsSelectionViewModel.kt:41)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
org.opentest4j.AssertionFailedError:
Expected :[DrillsType(drillType_title=title1, drillType_imageUrl=url1), DrillsType(drillType_title=title2, drillType_imageUrl=url2)]
Actual :null
<Click to see difference>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1124)
at com.nikolam.basketpro.ui.drills.selection.DrillsSelectionViewModelTest.drillTypeListWillBePopulatedOnSuccess(DrillsSelectionViewModelTest.kt:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native …Run Code Online (Sandbox Code Playgroud) 我正在尝试从github( https://github.com/JamesFT/Database-Quotes-JSON/blob/master/quotes.json )加载quotesJson文件。我是改造和所有这些的新手,所以我只是尝试遵循并理解教程(https://android.jlelse.eu/android-networking-in-2019-retrofit-with-kotlins-coroutines-aefe82c4d777)。如果这很愚蠢或者真的很简单,我非常抱歉。我仍在为此苦苦挣扎。如果我能得到一个简短的解释为什么它以其他方式完成,我将不胜感激!
我查看了 Retrofit 的文档,搜索了所有类似的溢出问题。问题是,如果我尝试更改
fun getQuotes(): Deferred<Response<QuoteResponse>>
为,
fun getQuotes(): Deferred<ResponseList<Quotes>>
则会出现错误
val quoteResponse = safeApiCall(...
private val okHttpClient = OkHttpClient().newBuilder()
.build()
fun retrofit() : Retrofit = Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://raw.githubusercontent.com/JamesFT/Database-Quotes-JSON/master/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
val quoteApi : QuoteApi = retrofit().create(QuoteApi::class.java)
}
Run Code Online (Sandbox Code Playgroud)
模型
val quoteAuthor : String,
val quoteText : String
)
// Data Model for the Response returned from the Api
data class QuoteResponse(
val results : List<Quote>
)
//A retrofit Network Interface for the Api
interface …Run Code Online (Sandbox Code Playgroud)