Replacing @ContributesIntoSet bindings for testing #778
Replies: 1 comment
-
This is working as intended. kotlin-inject is more lenient here but Metro does not leave room for ambiguity (for example - what if you contribute two different ones? who wins?)
This is expected, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm having difficulty in replacing
@ContributesIntoSet
bindings for my tests. It seems at@Contributes*
bindings are "strict", meaning that they conflict with other bindings directly in the DependencyGraph.Factory. This is an unexpected difference from kotlin-inject-anvil, where providing an explicitSet<T>
in the@MergeComponent
would take precedence over@ContributesBinding(multiple=true)
bindings.However, this same conflict does not happen for regular
@Inject
bindings -- providing a different instance in the DependencyGraph.Factory does take precedence.Is this expected? Is there an easier way to replace
@ContributesIntoSet
bindings than my workaround?Example
I have a basic plugin system in my app, with each plugin using
@ContributesIntoSet
:In my tests, I want to be able to test with a custom set of plugins. So I have this:
However, that gives me an error about duplicate bindings for
Set<Plugin>
:I could use
@DependencyGraph(..., excludes=[...exclude all plugins here...])
but then I have to hardcode all of those plugins, which seems to defeat the purpose of aggregation.Workaround
Instead, I need to add a wrapper class:
and change the factory create method:
And that works just fine. This overrides the default
PluginContainer
. But it's somewhat odd to need this wrapper class in the application, just to support testing.Beta Was this translation helpful? Give feedback.
All reactions