Hi, I try to create UI test with koin, fragment and viewModel (architecture component)
@RunWith(AndroidJUnit4::class)
class LoginFragmentTest{
@Before
fun init() {
startKoin(hsApp + viewModel + api)
activityRule.activity.setFragment(testFragment)
EspressoTestUtil.disableProgressBarAnimations(activityRule)
}
@Test
fun loading() {
testFragment.model.loginLoading.value = true
Espresso.onView(ViewMatchers.withId(R.id.login_button)).check(ViewAssertions.matches(ViewMatchers.isEnabled()))
}
}
@OpenForTesting
class LoginFragment : HSFragment() {
val TAG = javaClass.simpleName
val model: LoginViewModel by viewModel()
val modelConfig: ConfigViewModel by viewModel()
val router : Router by inject()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
model.loginSuccess.observe(this, Observer { response ->
Log.d(TAG, response.toString())
context?.let {
router.homeActivity(it)
}
})
model.failure.nonNull().observe(this) { it?.let { showError(it) } }
model.loginLoading.nonNull().observe(this) { state -> loginButton.isEnabled = !state}
}
}
I recived this error
java.lang.IllegalStateException: Can't access ViewModels from detached fragment at android.support.v4.app.Fragment.getViewModelStore(Fragment.java:255) at android.arch.lifecycle.ViewModelStores.of(ViewModelStores.java:60) at org.koin.android.architecture.ext.LifecycleOwnerExtKt.getViewModelByClass(LifecycleOwnerExt.kt:177) at org.koin.android.architecture.ext.LifecycleOwnerExtKt$viewModelByClass$1.invoke(LifecycleOwnerExt.kt:87) at org.koin.android.architecture.ext.LifecycleOwnerExtKt$viewModelByClass$1.invoke(Unknown Source:0) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.ooma.hm.ui.login.LoginFragment.getModel(Unknown Source:7) at com.ooma.hm.ui.login.LoginFragmentTest.loading(LoginFragmentTest.kt:74) at java.lang.reflect.Method.invoke(Native Method) 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 android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) at org.junit.rules.RunRules.evaluate(RunRules.java:20) 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.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) 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.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Maybe, Do you have any test with koin, fragment and viewModel
Hi, I try to create UI test with koin, fragment and viewModel (architecture component)
I recived this error
java.lang.IllegalStateException: Can't access ViewModels from detached fragment at android.support.v4.app.Fragment.getViewModelStore(Fragment.java:255) at android.arch.lifecycle.ViewModelStores.of(ViewModelStores.java:60) at org.koin.android.architecture.ext.LifecycleOwnerExtKt.getViewModelByClass(LifecycleOwnerExt.kt:177) at org.koin.android.architecture.ext.LifecycleOwnerExtKt$viewModelByClass$1.invoke(LifecycleOwnerExt.kt:87) at org.koin.android.architecture.ext.LifecycleOwnerExtKt$viewModelByClass$1.invoke(Unknown Source:0) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.ooma.hm.ui.login.LoginFragment.getModel(Unknown Source:7) at com.ooma.hm.ui.login.LoginFragmentTest.loading(LoginFragmentTest.kt:74) at java.lang.reflect.Method.invoke(Native Method) 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 android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) at org.junit.rules.RunRules.evaluate(RunRules.java:20) 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.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) 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.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Maybe, Do you have any test with koin, fragment and viewModel