DaggerMock icon indicating copy to clipboard operation
DaggerMock copied to clipboard

How to get test tear down in extended DaggerMockRule ?

Open theapache64 opened this issue 5 years ago • 2 comments
trafficstars

Is it possible to get a method callback to the customized DaggerMockRule? (Usage : I want to close the mocked db)

Current implementation

    @get:Rule
    val mockDbRule = DaggerMockDbRule()

    @After
    fun tearDown() {
        mockDbRule.db.close()
    }

DaggerMockDbRule.kt

class DaggerMockDbRule : DaggerMockRule<AppComponent>(AppComponent::class.java, DatabaseModule()) {

    var db: AppDatabase = Room.inMemoryDatabaseBuilder(
        ApplicationProvider.getApplicationContext(),
        AppDatabase::class.java
    ).build()

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

    init {
        customizeBuilder<DaggerAppComponent.Builder> {
            it.baseNetworkModule(BaseNetworkModule(App.BASE_URL))
                .contextModule(ContextModule(app))
        }

        set {
            it.inject(app)
        }

        provides(AppDatabase::class.java, db)
    }
}

theapache64 avatar Apr 28 '20 22:04 theapache64

Right now there aren't any callbacks, however if you need it you can try to override the apply method and add your call at the end of the Statement (after invoking base.evaluate())

fabioCollini avatar May 06 '20 06:05 fabioCollini

Okay. I'll give it a try and update here.

theapache64 avatar May 06 '20 06:05 theapache64