Alternative to Dagger's @Component.Builder
#564
kevinguitar
started this conversation in
General
Replies: 2 comments 3 replies
-
I think an example would give better context, with Dagger, we can override some bindings per test case by using builder functions in Component: interface Logger {
fun log(message: String)
}
@Inject
class LoggerImpl : Logger {
override fun log(message: String) = println(message)
}
@ContributesBinding(AppScope::class)
interface LoggerModule {
@Binds
fun bindLogger(impl: LoggerImpl): Logger
} In tests @MergeComponent(AppScope::class)
interface TestAppComponent {
@MergeComponent.Builder
interface Builder {
fun add(loggerModule: LoggerModule): Builder
// other functions to override modules...
fun build(): TestAppComponent
}
}
class MyTest {
@Test
fun testWithFakeLogger() {
val fakeLoggerModule = object : LoggerModule {
@Binds
override fun bindLogger(impl: FakeLogger): Logger
}
val testApp = DaggerTestAppComponent.builder()
.add(fakeLoggerModule)
.build()
// ...
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Builders aren't supported currently, nor are modules (the primary reason to have them). You could probably get decently far with |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello Zac 👋
I found out that there is no equivalent to Dagger's
@Component.Builder
in metro (written here), and we're using it to replace dependencies in our UI tests dynamically.Is there an alternative to Component Builder, perhaps some ways to workaround it? (Contribution replacement doesn't work for our case because we need to specify impls and build the graph dynamically per test case)
Beta Was this translation helpful? Give feedback.
All reactions