Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get test tear down in extended DaggerMockRule ? #103

Open
theapache64 opened this issue Apr 28, 2020 · 2 comments
Open

How to get test tear down in extended DaggerMockRule ? #103

theapache64 opened this issue Apr 28, 2020 · 2 comments

Comments

@theapache64
Copy link

theapache64 commented Apr 28, 2020

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)
    }
}
@fabioCollini
Copy link
Owner

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())

@theapache64
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants