https://kotlinlang.org logo
#kotest
Title
# kotest
m

Maxr1998

12/04/2021, 8:54 PM
I'm having some issues with the migration, notably with the Koin integration. The KoinExtension registers a
beforeAny
that filters
TestType.Test
, I previously declared my DI mocks in
beforeEach
so that I didn't have to do that extra check. However, with Kotest 5.0,
beforeEach
gets called before
beforeAny
in TestExtensions, which causes my mocks to fail because Koin hasn't started yet. I wonder if the order in TestExtensions should be changed again, as I'd expect the outer scopes to evaluate before inner scopes, thus container → any → each.
Additionally, the KoinExtension hasn't apparently been updated to 5.0.0 yet, as using the
KoinLifecycleMode.Root
causes a
NoSuchMethodException
.
s

sam

12/04/2021, 9:00 PM
Outer scope listeners will fire first
but perhaps what you're seeing here is the beforeAny of the inner firing before the beforeEach of the inner
m

Maxr1998

12/04/2021, 9:02 PM
I should've been more clear, I'm observing that beforeEach (which I register in the spec constructor) is called before the beforeAny that the KoinExtension uses.
Previously, that wasn't the case
s

sam

12/04/2021, 9:03 PM
Do you have an example layout ?
the test contents don't matter, just the structure
and the callbacks
m

Maxr1998

12/04/2021, 9:07 PM
Copy code
class Sample : DslDrivenSpec(), StringSpecRootScope, KoinTest {

    lateinit var mockedProperty: SessionRepository

    init {
        beforeEach {
            mockedProperty = declareMock()
        }

        "Some test case" {

        }
    }

    override fun extensions() = listOf(
        KoinExtension(
            modules = listOf(appModule, authModule),
            mockProvider = { clazz -> mockkClass(clazz) },
        ),
    )
}
That's an example of what I'm doing right now
The problem is that beforeEach with the declareMock is executed before the KoinExtension is run
s

sam

12/04/2021, 9:08 PM
Why are you extending dsl driven spec and root scope ?
m

Maxr1998

12/04/2021, 9:10 PM
Because my base class for tests does a few other things, just copy pasted this sample. It also fails when extending
StringSpec
normally.
s

sam

12/04/2021, 9:10 PM
ok
I makes sense that beforeAny should run before beforeEach since it's more general
But the trouble with trying to order beforeXX operations is that however you do it, someone somewhere will want it the other way.
Let me try to think of a better solution.
Probably moving the KoinExtension to be an interceptor will do the trick, then it will always run first.
m

Maxr1998

12/04/2021, 9:13 PM
But the trouble with trying to order beforeXX operations is that however you do it, someone somewhere will want it the other way.
True, it was just a bit surprising that it changed after the upgrade. But I agree a more general solution would be preferable.
Probably moving the KoinExtension to be an interceptor will do the trick, then it will always run first.
Is that something that I can do or does it have to be changed in the extension?
s

sam

12/04/2021, 9:15 PM
Yeah in the extension
But I can do that now and release a 5.0 compatible release
It won't take long assuming it works
m

Maxr1998

12/04/2021, 9:16 PM
That would be very appreciated, thank you!
Thanks for the update, looks promising! Gonna try it out tomorrow.
s

sam

12/04/2021, 11:33 PM
How did you know 🙂
Was about to tell you
m

Maxr1998

12/04/2021, 11:35 PM
Actually, I randomly opened the GitHub and saw you push like a minute ago. Very random and a bit scary 😆
s

sam

12/04/2021, 11:35 PM
haha
m

Maxr1998

12/04/2021, 11:36 PM
But yeah, I'll give it a go tomorrow / once it's published.
if you use latest (remember to add snapshots maven repo)
and it works, I will publish 1.1.0
m

Maxr1998

12/04/2021, 11:38 PM
Ok, I'll try and report back!
1
I think the default is still the per-test mode though?
s

sam

12/04/2021, 11:40 PM
yes, I just wanted to make it clear you could change it
the docs are somewhat lacking
I'm not even sure myself what the KoinTest interface does
m

Maxr1998

12/04/2021, 11:41 PM
Ah ok, just because the tests explicitly use
Test
in the second sample, thought it should maybe outline the root mode there.
KoinTest
is just a simple marker to allow resolving Koin components
It's actually just an alias to
KoinComponent
Might do other things too, but I haven't come across those yet
s

sam

12/04/2021, 11:44 PM
Ah ok
So it's needed? I don't know koin very well
I've updated the docs to reverse the KoinLifecycleMode example
m

Maxr1998

12/04/2021, 11:47 PM
It's necessary to use declareMock
If I find the time, I can open a PR for the docs tomorrow and make them a bit clearer
s

sam

12/04/2021, 11:53 PM
Yeah taht would be awesome
m

Maxr1998

12/05/2021, 6:22 PM
Update: new version works perfectly!
s

sam

12/05/2021, 6:22 PM
awesome
m

Maxr1998

12/05/2021, 6:23 PM
When are you planning to release it to the stable channel then?
s

sam

12/05/2021, 6:29 PM
can do it now
m

Maxr1998

12/05/2021, 6:33 PM
Nice. I just opened a PR for the docs. Conceptually, it was fine, but the code sample had a small mistake and I made the lifecycle mode option a bit clearer.
Open for suggestions though.
s

sam

12/05/2021, 6:34 PM
looks good
m

Maxr1998

12/05/2021, 6:37 PM
Thanks for the merge!
s

sam

12/05/2021, 6:41 PM
1.1.0 released, takes 30 minutes to show up
1
2 Views