https://kotlinlang.org logo
#mockk
Title
# mockk
d

Dariusz Kuc

06/12/2019, 11:00 PM
hello! it appears that it is not possible to capture arguments to mocked methods based on the other arguments - is that expected? e.g.
Copy code
class MockkCaptorTest {

    @Test
    fun `verify mockk captor behavior`() {
        val simpleMockk = mockk<Simple> {
            every { hello(any(), any()) } answers { "Mocked response for ${arg<String>(0)}, age: ${arg<Int>(1)}" }
        }

        simpleMockk.hello("Bob", 2)
        simpleMockk.hello("Alice", 1)

        val bobsAge = slot<Int>()
        verify {
            simpleMockk.hello("Bob", capture(bobsAge))
        }
        assertEquals(2, bobsAge.captured)
    }
}

class Simple {
    fun hello(name: String, age: Int) = "Hello $name, age: $age"
}
above will actually capture
Alice
age
note: mockito handles this fine
l

LeoColman

06/12/2019, 11:48 PM
i'm not sure you're using capture slots correctly
d

Dariusz Kuc

06/12/2019, 11:50 PM
me neither, thats why I'm asking
l

LeoColman

06/12/2019, 11:53 PM
I think the capture should happen on the
every
block
and then you'll use the slot for verifications
However I'm not sure how you can link those parameters
d

Dariusz Kuc

06/13/2019, 12:47 AM
well i can pass a mutable list for capturing slots that will capture all of the args but again my problem is that I would have to explicitly capture all of the params
and then iterate over the first one to find out the index of the one i'm interested in
if thats the only option then it is an oversight
3 Views