https://kotlinlang.org logo
Title
p

Patrick Ramsey

04/13/2021, 5:26 AM
Say I have a verify block like this:
verify {
    someMethod(capture(mySlot), "Some specific value")
}
Is there any way to actually just capture the value from when someMethod() was called with “Some specific value”? Or will that capture() match every single call, even the ones where the whole expression doesn’t match?
e

ephemient

04/13/2021, 5:46 AM
works on the mocking side,
p

Patrick Ramsey

04/13/2021, 5:46 AM
hmm?
So, say, in the code under test, someMethod ends up getting called twice:
someMethod(3, “Some specific value”) someMethod(4, “some other string”)
e

ephemient

04/13/2021, 5:47 AM
val slot = mutableListOf<T>()
val obj = mockk {
    justRun { someMethod(any(), any()) }
    justRun { someMethod(capture(slot), eq("Some specific value")) }
}
obj.someMethod("foo", "bar")
obj.someMethod("capture me", "Some specific value")
p

Patrick Ramsey

04/13/2021, 5:47 AM
you’d expect mySlot to contain 3, but it’ll actually be set to 4
e

ephemient

04/13/2021, 5:49 AM
you could file an issue about it not working on the verify side, but consider fixing it yourself
p

Patrick Ramsey

04/13/2021, 5:49 AM
https://github.com/mockk/mockk/issues/352 so since posting I found this issue
e

ephemient

04/13/2021, 5:50 AM
that's not the same thing
p

Patrick Ramsey

04/13/2021, 5:50 AM
it looks like in more recent mockk, it warns when you do this. But unfortunately that still means there isn’t to my knowledge a good way to match a certain call, then get the arguments from that call
e

ephemient

04/13/2021, 5:50 AM
and is why I used MutableList to capture
p

Patrick Ramsey

04/13/2021, 5:50 AM
right, but
e

ephemient

04/13/2021, 5:50 AM
there is, match it in mockk (every / justRun)
p

Patrick Ramsey

04/13/2021, 5:50 AM
if you use MutableList, you just end up with a list of matched parameters, right?
e

ephemient

04/13/2021, 5:51 AM
it's just the matchers in verify that don't seem to operate in the right order
p

Patrick Ramsey

04/13/2021, 5:51 AM
you don’t know which values corresponded to which calls
ah. Let me see if I can structure this to do it from every
but just to make sure I understand: using a capture() in a verify { } block means you’re effectively matching every call, even if there are other matchers that are more specific
e

ephemient

04/13/2021, 5:52 AM
p

Patrick Ramsey

04/13/2021, 5:52 AM
OOOO.
awesome! Okay.
e

ephemient

04/13/2021, 5:53 AM
yes, it's a bug, there's not enough manpower to fix mockk, consider fixing it yourself
p

Patrick Ramsey

04/13/2021, 5:53 AM
ok! Good to know
thanks, @ephemient
sorry to bother you
e

ephemient

04/13/2021, 5:54 AM
np. hope matching in every {} is a usable workaround for the time being
(I don't work on mockk, I just use it too. and run into other issues...)
p

Patrick Ramsey

04/13/2021, 5:55 AM
yeah, I think so.
and thank you for popping up and helping anyway 🙂 and I’ll consider contributing a fix. Would be nice to contribute back to a project I use every day 🙂
thanks again, and have a good night!
👋 1
m

Mattia Tommasone

04/13/2021, 7:21 AM
thanks a lot to both of you for the discussion and the help 🙂 the behavior described in #408 indeed looks like what @Patrick Ramsey is experiencing, so basically,
capture
should be capturing only calls that are actually matched by a verify block and not all calls. It is indeed a bug; i may try taking a shot at fixing it soon, but if in the meantime you want to start looking into it i’d be more than glad, feel free to shoot any questions 🙂