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
Testing with dagger #1052
Comments
The first solution, which comes to mind is to generate a component designed for testing, with methods to substitute every single provider and to get every service. More detailed description:
If no services have been substituted in the builder, the testing component should behave exaclty as production component P.S. If such (or some other) design is accepted, I may try to implement a PR |
@Dunemaster - I tried creating another component that extends the base component (which is the suggested path), but I am getting Duplicate class errors in compilation because of the sub components. Is this something you've also run into? |
I'm following these steps to override Modules of a Component (using the new Factory approach The
The Module looks like:
The interface and Implementation:
An
Then in our
Then in your
The idea behind this is to have access to the implementation behind the |
@luislukas By creating and holding on to @Module
class SomeModule {
@VisibleForTesting
var some : Some = SomeImpl()
@Provides
fun some(): Some = some
} Also, if @Module
class SomeModule {
@VisibleForTesting
lateinit var some: Some
@Provides
fun some(dependencyAOfSomeImpl: DependencyA): Some {
if (!::some.isInitialized) { // Skip this check if you don't want @Singleton behaviour
some = SomeImpl(dependencyAOfSomeImpl)
}
return some
}
} |
I think #186 also has discussion on this topic. We're aware of testing being a problem with Dagger in general and our approach is going to be to improve this with Hilt. Hilt only works for Android right now, but I think we see some of the testing solutions there as eventually being applicable to non-Android cases as well. |
I would like to discuss testing with dagger (continuing #110).
As it stands, testing with dagger is hard, requiring a log of boilerplate code and/or altering system design just to facilitate testing, especially when subcomponents and abstract modules are involved (For eaxmple, the approaches described in https://google.github.io/dagger/testing.html ) .
There are frameworks like https://github.com/fabioCollini/DaggerMock, which try to solve the problem, but there have rather limited applicability. For example, DaggerMock does not support abstract modules (thought, I must say, DaggerMock is just an excellent work).
Basically, my requirments for testing fall into two categories:
Currently, there is not elegant way to do (1) and no way to implement (2) (if you dont count creating separate modules for every single services)
The text was updated successfully, but these errors were encountered: