Say I have a verify block like this: ```verify { ...
# mockk
p
Say I have a verify block like this:
Copy code
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
works on the mocking side,
p
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
Copy code
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
you’d expect mySlot to contain 3, but it’ll actually be set to 4
e
you could file an issue about it not working on the verify side, but consider fixing it yourself
p
https://github.com/mockk/mockk/issues/352 so since posting I found this issue
e
that's not the same thing
p
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
and is why I used MutableList to capture
p
right, but
e
there is, match it in mockk (every / justRun)
p
if you use MutableList, you just end up with a list of matched parameters, right?
e
it's just the matchers in verify that don't seem to operate in the right order
p
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
p
OOOO.
awesome! Okay.
e
yes, it's a bug, there's not enough manpower to fix mockk, consider fixing it yourself
p
ok! Good to know
thanks, @ephemient
sorry to bother you
e
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
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
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 🙂