簡體   English   中英

Android單元測試模擬firebase

[英]Android Unit test mock firebase

我正在啟動viewModel的單元測試,並且需要注入存儲庫依賴項,但是當我運行第一個測試來驗證Koin是否正在運行時,拋出異常:

org.koin.core.error.InstanceCreationException:無法為[type:Single,primary_type:'com.ramattec.meussaloes.data.repository.service.ServiceRepository']創建實例

at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61)
at org.koin.core.instance.SingleDefinitionInstance.get(SingleDefinitionInstance.kt:40)
at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)
at org.koin.core.scope.Scope.get(Scope.kt:128)
at com.ramattec.meussaloes.ui.services.ServicesViewModelTest$$special$$inlined$inject$1.invoke(KoinTest.kt:51)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.ramattec.meussaloes.ui.services.ServicesViewModelTest.getRepository(ServicesViewModelTest.kt)
at com.ramattec.meussaloes.ui.services.ServicesViewModelTest.Verificar se Koin está funcionando corretamente(ServicesViewModelTest.kt:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

原因:java.lang.IllegalStateException:此過程未初始化默認的FirebaseApp。 確保首先調用FirebaseApp.initializeApp(Context)。 在com.google.firebase.database.FirebaseDatabase.getInstance(com.google.firebase:firebase-firemon:firebase-common @@ 17.1.0:186)上的com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common @@ 17.1.0:186) @@ 17.0.0:56)com.ramattec.meussaloes.data.repository.service.ServicesRepositoryImpl。(ServicesRepositoryImpl.kt:19)com.ramattec.meussaloes.ui.services.ServicesViewModelTest $$ 1 $ 1 $ 1.invoke( com.ramattec.meussaloes.ui.services.ServicesViewModelTest $ before $ 1 $ 1 $ 1.invoke(ServicesViewModelTest.kt:27)上的ServicesViewModelTest.kt:38),位於org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54) )

@RunWith(MockitoJUnitRunner::class)
class ServicesViewModelTest: KoinTest{

    private val repository: ServiceRepository by inject()
    private val servicesViewModel: ServicesViewModel by inject()

    @Before
    fun before(){
        startKoin {
            modules(
                module {
                    single<ServiceRepository> { ServicesRepositoryImpl()}
                    single { ServicesViewModel(get()) }
                })
        }
    }

    @Test
    fun `Verify if Koin is running`(){
        assertEquals(repository, servicesViewModel.repository)
    }
}

我願意接受建議

您無需模擬甚至不對Firebase進行單元測試,而無需對模型和DAO進行單元測試。

就我而言,我不需要注入存儲庫,正確的是:

@Mock
private val repository: ServiceRepository
private val servicesViewModel: ServicesViewModel by inject()

@Before
fun before(){
    startKoin {
        modules(
            module {
                single { ServicesViewModel(repository) }
            })
    }
}

現在,我可以為ViewModel編寫測試了! 感謝您的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM