类型推断失败:没有足够的信息来推断 fun <T : Context!> getApplicationContext() 中的参数 T:T!请明确说明
Type inference failed: Not enough information to infer parameter T in fun <T : Context!> getApplicationContext(): T! Please specify it explicitly
我正在尝试为我的 android 应用程序编写一些测试,这对我来说真的很麻烦。许多障碍之一就是这个错误
Type inference failed: Not enough information to infer parameter T in fun <T : Context!> getApplicationContext(): T! Please specify it explicitly.
出现在这一行
val actualIntent: Intent = shadowOf(ApplicationProvider.getApplicationContext())
.nextStartedActivity
完整的测试代码如下所示
@Test
fun clickingLogin_shouldStartLoginActivity() {
val scenario = launch(LogInActivity::class.java)
scenario.onActivity { activity ->
activity.go_to_register_button.performClick()
val expectedIntent = Intent(activity, RegistrationActivity::class.java)
val actual: Intent = shadowOf(ApplicationProvider.getApplicationContext())
.nextStartedActivity
expectedIntent.component shouldBeEqualTo actual.component
}
}
基本上 shadowOf 函数是重载的,return 许多人认为我需要指定类型。
我认为它应该类似于 shadowOf<SomeType>(...)
但我不知道实际类型应该是什么。
任何帮助将不胜感激。
编辑
我正在关注 roboloctric guideline 但试图以 androidX 方式
编写它
Intent
是一种不同类型的 Object
,它不从 Context
延伸。
这一行:
val actualIntent: Intent = shadowOf(ApplicationProvider.getApplicationContext())
提供了一个 Context
作为参数,returns 提供了一个 ShadowContext
,而不是 Intent
.
文档参考:http://robolectric.org/javadoc/3.0/org/robolectric/Shadows.html#shadowOf-android.content.Context-
基本上就是告诉你,树不能是车的一种。
我可能问的不够清楚。但是对于任何遇到同样问题的人来说,这里有一个解决方案。
我不知道如何测试 activity 在正常测试中是否产生正确的意图。但在仪器测试中它是这样的:
@get:Rule
var activityRule: IntentsTestRule<MyActivity> =
IntentsTestRule(MyActivity::class.java)
@Test
fun testIntent () {
// perform some actions
// than verify
intended(hasComponent(OtherActicity::class.qualifiedName))
intended(hasExtra(A_CONSTANT, someValue))
}
你需要一个依赖项才能工作
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
我正在尝试为我的 android 应用程序编写一些测试,这对我来说真的很麻烦。许多障碍之一就是这个错误
Type inference failed: Not enough information to infer parameter T in fun <T : Context!> getApplicationContext(): T! Please specify it explicitly.
出现在这一行
val actualIntent: Intent = shadowOf(ApplicationProvider.getApplicationContext())
.nextStartedActivity
完整的测试代码如下所示
@Test
fun clickingLogin_shouldStartLoginActivity() {
val scenario = launch(LogInActivity::class.java)
scenario.onActivity { activity ->
activity.go_to_register_button.performClick()
val expectedIntent = Intent(activity, RegistrationActivity::class.java)
val actual: Intent = shadowOf(ApplicationProvider.getApplicationContext())
.nextStartedActivity
expectedIntent.component shouldBeEqualTo actual.component
}
}
基本上 shadowOf 函数是重载的,return 许多人认为我需要指定类型。
我认为它应该类似于 shadowOf<SomeType>(...)
但我不知道实际类型应该是什么。
任何帮助将不胜感激。
编辑 我正在关注 roboloctric guideline 但试图以 androidX 方式
编写它Intent
是一种不同类型的 Object
,它不从 Context
延伸。
这一行:
val actualIntent: Intent = shadowOf(ApplicationProvider.getApplicationContext())
提供了一个 Context
作为参数,returns 提供了一个 ShadowContext
,而不是 Intent
.
文档参考:http://robolectric.org/javadoc/3.0/org/robolectric/Shadows.html#shadowOf-android.content.Context-
基本上就是告诉你,树不能是车的一种。
我可能问的不够清楚。但是对于任何遇到同样问题的人来说,这里有一个解决方案。
我不知道如何测试 activity 在正常测试中是否产生正确的意图。但在仪器测试中它是这样的:
@get:Rule
var activityRule: IntentsTestRule<MyActivity> =
IntentsTestRule(MyActivity::class.java)
@Test
fun testIntent () {
// perform some actions
// than verify
intended(hasComponent(OtherActicity::class.qualifiedName))
intended(hasExtra(A_CONSTANT, someValue))
}
你需要一个依赖项才能工作
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'