-
-
Notifications
You must be signed in to change notification settings - Fork 720
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
Multiple bean definitions (Conflicts) #100
Comments
If you want to reuse a module and override some definitions for your test, just make a module containing your new definitions and load modules in order: your app + your test module The last module will override needed definitions A bean is overriden when: same name, type and scope You can match multiple bean for a given type if you use |
This project sample provides a test module: https://github.com/Ekito/koin-samples/tree/master/samples/android-weatherapp-mvvm/app/src/test/java/org/koin/sampleapp |
I wish that worked!
When I tried what you described, I get this exception:
https://github.com/Ekito/koin/blob/0.9.1/koin-core/src/main/kotlin/org/koin/KoinContext.kt#L121
…-W
On Wed, Apr 4, 2018, 12:58 Arnaud Giuliani ***@***.***> wrote:
This project sample provides a test module:
https://github.com/Ekito/koin-samples/tree/master/samples/android-weatherapp-mvvm/app/src/test/java/org/koin/sampleapp
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#100 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AHEEtzaX6gI5IX7Y3MCmitU3ySqckQNAks5tlPuRgaJpZM4TBrpM>
.
|
Do you have any sources to provide? |
Could help me better understand your case |
It's really hard to extract out a MWE for this. I think I can illustrate using the sample app you provided... Given this line... You have Unlike in this sample, My TestApplication had something like
There was overlap between the classes in
My workaround was basically this
|
If I understand, if you provide a dedicated mock module Or do you do something in some constructors? |
Correct, startKoin fails when I add a dedicated module containing mock instances. (Only when 1 or more mocks instances replace a real instance. If there's no collision, there's no exception) I did not use All of my injection is eager & constructor based (except my Android activities, which use |
Original conflict comes from here. making it eager at start don"t help you, even more, if you use android context at start. |
Of course you are correct about As you mentioned, the root cause appears to be a choice of eager-injection versus lazy-injection. I still prefer "immutable-after-construction" & eager injection, so I'll use the workaround above. Thanks for the helpful information. |
Your documentation says "latest bean wins"
(See https://insert-koin.io/docs/1.0/reference/koin-dsl/?#multiple-definitions)
The source code throws an exception if there are multiple beans:
see https://github.com/Ekito/koin/blob/0.9.1/koin-core/src/main/kotlin/org/koin/KoinContext.kt#L121
In case future readers face this,
My use case:
I want to override/replace/shadow specific beans with mocks/spies in my unit/integration tests.
My solution was to
BeanDefinition
instance for each mock/spy/fakekoin.beanRegistry.declare( theBeanDef , Scope.root())
The text was updated successfully, but these errors were encountered: