is there a way to override koin dependencies in te...
# koin
m
is there a way to override koin dependencies in tests? (eg. I don't want http client making request to 3rd party in tests)
https://insert-koin.io/docs/reference/koin-core/modules/#overriding-definition-or-module-310 example here shows it, but how do I load that module only in tests?
ended up adding an env variable that represents the environment (eg. "test", "prod") and then did:
Copy code
if (environment.config.property("environment").getString() == "test") {
    koinModules.add(module { loadTestDependencies() })
}
not sure if there is a better way?
a
just add an extra module with overriden definitions in your test would work
m
how can I add it in test?
if you see my last answer that is what I ended up with,
if there is a config that says we are running tests, we load test module that is predefined
a
ok, you can also load new modules if necessary with
loadKoinModules()
function
m
in tests? I tried that while doing a ktor test, but I couldn't find a way to load it
Copy code
class CustomerRoutesTest : KoinTest {
    @Test
    fun `create a new customer`() {
        withApplication(withTestEnvironment()) {
            with(
                handleAuthenticatedRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/customers") {
                    setBody(createRequest())
                }
            ) {
                assertEquals(Created, response.status())
            }
            // assert stuff
        }
    }
}
this is a test, when would I call
loadKoinModules
here?
a
ho yeah, for Ktor not sure it apply 🤔
m
its still extending
KoinTest
if that helps in any case?
but ye, I tried loading module when doing these kind of tests, but it wasn't overridden
a
what version are you using?
101 Views