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

Koin test - moduleCheck for parametered injections #274

Closed
miszmaniac opened this issue Oct 15, 2018 · 5 comments
Closed

Koin test - moduleCheck for parametered injections #274

miszmaniac opened this issue Oct 15, 2018 · 5 comments
Labels
Milestone

Comments

@miszmaniac
Copy link

Is your feature request related to a problem? Please describe.
I was trying to mock MVP, where View and Presenter are both injected from activity:

class SampleKoinActivity : Activity {
    override val presenter: SampleKoinPresenter by inject { parametersOf(this) }
    override val view: SampleKoinView by inject { parametersOf(this) }
}

Where Di module looks like that:

   factory { (activity: AppCompatActivity) -> SampleKoinView(activity) }
   factory { (activity: SampleKoinActivity) -> SampleKoinPresenter(activity.view, get()) }

But moduleCheck always fail with:

org.koin.error.NoParameterFoundException: Can't get parameter value #0 from org.koin.core.parameter.ParameterList@36b6964d

Of course code works in activity.

Describe the solution you'd like
I don't know what would be the best solution here. Maybe some way of injecting things from parameters, like below?

Describe alternatives you've considered
I've tried to mock those input parameters, hoping Koin would inject those instead of parameters.

      val contextModule = module {
            single { mock(Application::class.java) }
            single { mock(Context::class.java) }
            single { mock(View::class.java) }
            single { mock(AppCompatActivity::class.java) }
        }
        checkModules(listOf(contextModule) + allModules)

Target Koin project
koin-test

@arnaudgiuliani arnaudgiuliani added test question Usage question labels Oct 16, 2018
@arnaudgiuliani
Copy link
Member

weird, parameters should be ignored :/

Your test is failing or is it an error log?

@miszmaniac
Copy link
Author

No, test doesn't fail, but checkModule isn't asserting anything it only logs errors to console.
I've changed implementation of logger with:

override fun err(msg: String) {
            Assert.fail(msg)
        }

This goes into logs:

2018-10-18 09:50:36:291 (KOIN)::[i] +-- 'com.infullmobile.android.infullmvp.sample.temperatureCard.TemperatureCardPresenter' @ temperatureCardScopeName
2018-10-18 09:50:36:291 (KOIN)::[e] sandbox ~ intercepted error : org.koin.error.NoParameterFoundException: Can't get parameter value #0 from org.koin.core.parameter.ParameterList@4248b963
2018-10-18 09:50:36:308 (KOIN)::[i] \-- (*) Created

Where this module is defined this way:

const val temperatureCardScopeName = "temperatureCardScopeName"
val temperatureCardModule = module(temperatureCardScopeName) {

    factory { (customView: TemperatureCard) -> TemperatureCardPresenter(customView.presentedView, get()) }
    factory { (view: View) -> TemperatureCardView(view) }
}

@arnaudgiuliani arnaudgiuliani added type:issue status:accepted accepted to be developed and removed question Usage question labels Oct 19, 2018
@arnaudgiuliani arnaudgiuliani added this to the 1.0.2 milestone Oct 19, 2018
@arnaudgiuliani
Copy link
Member

Sandbox behind checkModules is still working. It's just a log level problem.
fixed in 1.0.2-alpha-8

@permenko
Copy link

permenko commented Nov 9, 2018

@arnaudgiuliani
Why parameters should be ignored ?
This test will pass, but it's not workable code.

@Test
fun paramModuleTest() {
    checkModules(listOf(
        module {
            factory { (usefulParam: UsefulParam) -> A(usefulParam) }
            factory { B(get()) }
        }))
}

class UsefulParam
class A(usefulParam: UsefulParam)
class B(a: A)

@arnaudgiuliani
Copy link
Member

CheckModules is just here to test links between definitions.
A dryRun would be more necessary to run every definition with default data/parameters.

This is not the same use.

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

No branches or pull requests

3 participants