0

我开始使用hilt,在测试时遇到了问题。

这是生成以下错误的代码:

@HiltAndroidTest
@RunWith(CommonUiRunner::class)
class PhoneEditTextTest {

    @Test
    fun testThatNumberFormatted() {
        val controller = Robolectric.buildActivity(AppCompatActivity::class.java)
        val activity = controller.get()
        val editText = PhoneEditText(activity)

        activity.setContentView(editText)
        controller.setup()
    }
}

@Module
@InstallIn(ApplicationComponent::class)
object ApplicationModule {

    @Provides
    @Singleton
    fun providePhoneNumberUtil(app: Application): PhoneNumberUtil = PhoneNumberUtil.createInstance(AssetsMetadataLoader(app.assets))
}

@Module
@InstallIn(ApplicationComponent::class)
object CountriesFeatureModule {

    @Provides
    @Singleton
    fun providesApi(app: Application): CountriesFeatureApi = CountriesFeatureImpl(app.assets)
}

返回:

APPLICATION and COUNTRIES-FEATURE modules contains in INJECTOR gradle-module.

你知道如何解决这个问题吗?

4

1 回答 1

0

是的,当你想测试匕首的东西时,你不能使用正常的活动。

请查看以下教程,因为它应该可以解决您的问题:https ://www.youtube.com/watch?v=k4zG93ogWFY&t

于 2020-09-05T20:15:01.947 回答