GiH
GiH

Reputation: 415

Match argument object that has a property equal to (using Mockk)

I have looked around for a similar question, but cannot find a solution.

I have a couple of instances to the same type of object. As a simple example, a Pen object. This class contains size (Int) and color (String) properties. I need to mock a function (such as calculatePrice) that takes in this type of object. I would like it to return a result based on one of the properties.

    every { calculatePrice(pen : Pen) } returns 3
    every { calculatePrice(pen2 : Pen) } returns 4

Because this call is nested within another function, I cannot guarantee that the address of the object is the same, so I am attempting to get around this by checking a property value, such as pen.size. If there is a way to make sure the value of the object is the same, not the reference, that would solve my problem too.

Using Mockito, I would use argThat(pen -> pen.size() == 2)

Any recommendations would be much appreciated.

Upvotes: 14

Views: 10150

Answers (1)

Cash Lo
Cash Lo

Reputation: 1210

match should work, see https://mockk.io/#matchers

For example:

every { calculatePrice( match { it.size == 2 } ) } returns 3

Upvotes: 22

Related Questions