将Dagger与Espresso结合使用 [英] Using Dagger with Espresso
问题描述
我打算在我的应用程序多模块上创建Espresso测试,并且即将创建第一个Espresso测试,但是我看到的是在我的应用程序上我没有 AppComponent
在哪里可以伪造.由于要在功能模块上添加测试,因此从现在开始我将在其中创建 TestApp
, TestRunner
.
I'm planning to create Espresso tests on my app multi-module, and I'm about to create the first Espresso test, but what I'm seeing is that on my app I do not have an AppComponent
where I can fake it. Since I want to add the test on my feature-module, I'll create the TestApp
, TestRunner
there from now.
我的功能模块上有一个 FeatureComponent
,它是通过 ComponentFactory
从 App
注入的,所以我想创建一个像这样的类:
What I have on my feature-module is a FeatureComponent
that is injected via ComponentFactory
from the App
, so what I thought is to create a class like this :
@Component (
dependencies = [ MoreComponents::class],
modules = [ DataSourceModule::class ]
)
interface FeatureOneComponent {
fun activityOneSubComponent(): FeatureOneActivity.Component.Factory
fun activityTwoSubComponent(): FeatureTwoActivity.Component.Factory
@Component.Factory
interface Factory {
fun create(
dependencies
):FeatureOneComponent
}
}
interface FeatureOneProvider {
fun getFeatureOneComponent(): FeatureOneComponent
}
///ACTIVITY
class FeatureOneActivity : AppCompatActivity() {
//this comes from Subcomponent is what I want to MOCK
@Inject lateinit var presenter
//these comes from the factory and I have it mocked
@Inject lateinit var manager
override fun onCreate(){
(applicationContext as FeatureOneProvider).getFeatureOneComponent().activityOneSubComponent().create(this).inject(this)
}
}
@Subcomponent(modules = [ActivityOneModule::class]) <--- THIS I WANT TO MOCK
interface Component {
fun inject(activity: FeatureOneActivity)
@SubComponent.Factory
interface Factory {
fun create(@BindsInstance activity: FeatureOneActivity): Component
}
}
@Module
interface ActivityOneModule {
@Binds
fun bindPresenter(impl: PresenterImpl): Contract.Presenter
}
测试
class MyTestApp : Application(), FeatureOneProvider {
override fun getFeatureOneComponent(): FeatureOneComponent {
return DaggerMockFeatureOneComponent.create()
}
}
@Component(
modules = [MockFeatureOneModules::class]
)
interface MockFeatureOneComponent : FeatureOneComponent {
//I NEED TO MOCK THE SUBCOMPONENT WITH `MockSubcomponent`
}
@Component
object MockFeatureOneModules {
@Provides
fun providesManager() : MyManager = mock(MyManager)
}
//I want to use this module to replace the subcomponent of my activity
@Module
object MockSubcomponent() {
@Provides
fun providesFakePresenter() : FeatureOneContract.Presenter = mock { FeatureOneContract.Presenter::class.java }
}
为了更好地理解问题
当我运行测试并放置调试器点时,我看到除了Presenter之外,其他所有东西都被嘲笑了,这是因为Presenter在其中
To better understand the problem
When I run my test and I put a debugger point I see everything is mocked but the Presenter, and that's because the presenter is in
@Subcomponent(modules = [ActivityOneModule::class]
interface Component {
fun inject(activity: FeatureOneActivity)
@SubComponent.Factory
interface Factory {
fun create(@BindsInstance activity: FeatureOneActivity): Component
}
}
@Module
interface ActivityOneModule {
@Binds
fun bindPresenter(impl: PresenterImpl): Contract.Presenter
}
在我的测试组件中,我无权访问覆盖"这个子组件,所以一切都被嘲笑了,但是这个子组件,我需要被嘲笑.
And in my test component I don't have access to "override" this subcomponent so everything is mocked but this subcomponent and I need this mocked.
推荐答案
我不知道这是否是最好的主意,但如果我没有误解您的意思,则您希望此Presenter返回 mock {}
>.您可以做的更改是:
I don't know if that's the best idea but if I did not misunderstand you you want this Presenter to return a mock {}
. The changes you could do are :
- 在您的TestComponent中,将
界面
更改为抽象类
- 复制您的子组件,并从真正的子组件开始扩展
@Component(
modules = [MockFeatureOneModules::class]
)
abstract class MockFeatureOneComponent : FeatureOneComponent {
abstract fun subcomponent() : MockComponent.FactoryMock
override fun activityOneSubComponent(): FeatureOneActivity.Component.Factory {
return subcomponent()
}
@Subcomponent
interface MockComponent : FeatureOneActivity.Component {
@Subcomponent.Factory
interface FactoryMock : FeatureOneActivity.Component.Factory {
override fun create....
}
}
}
就是这样,它应该可以工作.
And that's it, it should work.
这篇关于将Dagger与Espresso结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!