DaggerMock icon indicating copy to clipboard operation
DaggerMock copied to clipboard

When I use AndroidInjection the mock doesn't work

Open charleston10 opened this issue 7 years ago • 3 comments
trafficstars

When I use AndroidInjection the mock doesn't t work Example

/* FooActivity */
override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
    }

When I inject by AppComponent the mock is work

/* AppComponent */
...
interface AppComponent {

    fun inject(fooActivity: FooActivity)

@Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        fun appModule(appModule: AppModule): Builder
        fun networkModule(networkModule: NetworkModule): Builder
        fun build(): AppComponent
    }

...

My Rule

val app: App get() = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as App

fun espressoDaggerMockRule() = DaggerMock.rule<AppComponent>(
        AppModule(app), 
        MockNetworkModule() //doesn't use this class when I use AndroidInjection
) {
    provides(MockNetworkModule())
    set { component -> app.component = component; }
    customizeBuilder<AppComponent.Builder> { it.application(app) }
}

daggermock: 0.8.4 dagger: 2.11

charleston10 avatar Feb 21 '18 20:02 charleston10

Hi, are you using something to open the classes in the tests? You can find more info here: https://github.com/fabioCollini/DaggerMock#kotlin-support

Can you explain better the problem? Do you get any errors?

fabioCollini avatar Feb 23 '18 18:02 fabioCollini

Were u able to fix this, I can't seem to get the mocks working no build or runtime errors. I have a mix for kotlin and java. I think the problem is ModuleOverrider can't understand ActivityBuilder

sarmadali20 avatar Mar 01 '18 18:03 sarmadali20

Hi, what do you mean with ActivityBuilder? Can you add more informations to reproduce this problem? Thanks for your report

fabioCollini avatar Mar 10 '18 20:03 fabioCollini