Late init字段不会插入片段中。
我进行了以下测试:
@RunWith(RobolectricTestRunner::class)
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
class SampleTest {
@Test
fun checkFragmentProperty() {
launchFragmentInHiltContainer<TestFragment> {
// nothing
}
}
}
我的TestFragment看起来像这样:
@AndroidEntryPoint
class TestFragment : Fragment() {
@Inject
lateinit var testClass: TestClass
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
testClass.some()
}
}
当我尝试运行此测试时,它将显示错误kotlin.UninitializedPropertyAccessException: lateinit property testClass has not been initialized
我使用official documentation中的launchFragmentInHiltContainer
方法,但是当我只使用lunchFragmentInContainer
中的androidx.fragment.app.testing
方法时,效果相同。
当我使用仿真器而不是Robolectric时,字段将被正确注入。
我尝试添加:
hilt {
enableTransformForLocalTests = true
}
并从控制台运行测试,但测试仍然失败