本文介绍了espresso ActivityTestRule用于具有通用类型参数的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经宣布了这样的活动
I have declared an activity like this
class QuestionnaireActivity<T : ProfileModel> : AppCompatActivity()
我想写一个浓缩咖啡测试,所以我写的是ActivityTestRule
I want to write an espresso test so I'm writting ActivityTestRule like
@Rule @JvmField
val activityRule = object : ActivityTestRule<QuestionnaireActivity<ProfileModel.PersonalInfo>>(QuestionnaireActivity<ProfileModel.LifeStyleInfo>::class.java){
override fun getActivityIntent(): Intent = QuestionnaireActivity.getQuestionnaireIntent(InstrumentationRegistry.getTargetContext(), 3, ProfileModel.LifeStyleInfo())
}
但是编译器抱怨(关于ActivityTestRule的参数)
but the compiler complains that(its about the argument of ActivityTestRule)
only classes are allowed on the left hand side of a class literal
此处指出,泛型不能与 class 一起使用.
It is stated here that generics can't be used with class.
如果删除通用类型参数,错误将变为
If I remove the generic type parameter the error becomes
Type inference failed.
Expected type mismatch: inferred type is Class<QuestionnaireActivity<*>> but Class<QuestionnaireActivity<ProfileModel.PersonalInfo>!>! was expected
我该怎么办?
感谢您的关注
推荐答案
类似于此处解决方案包括定义这样的内联函数
similar to here the solution involves defining an inline function like this
inline fun <reified T: Activity> activityTestRuleWithIntent(intent: Intent) = object : ActivityTestRule<T>(T::class.java){
override fun getActivityIntent(): Intent = intent
}
则规则变为:
@Rule @JvmField
val rule = activityTestRuleWithIntent<QuestionnaireActivity<ProfileModel.LifeStyleInfo>>(QuestionnaireActivity.getQuestionnaireIntent(InstrumentationRegistry.getTargetContext(), 3, ProfileModel.LifeStyleInfo()))
这篇关于espresso ActivityTestRule用于具有通用类型参数的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!