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

Mattia Tommasone

04/08/2021, 2:47 PM
uhm not sure i understand what you’re trying to achieve do you want to return a different employeeAttribute depending on the value of the parameter being passed to getEmployee?
i

ivano

04/08/2021, 2:58 PM
and I need for every getEmployee(“1”) return a specific object, a data class, and for getEmployee(“3") i need to return a data class with different values
yes I want on the base of the id parameter getting passed to return a different
EmployeeAttribute
is working
I have to pass a different every{} returns for each id
sorry for the silly question
m

Mattia Tommasone

04/08/2021, 3:01 PM
yep you can use two different
every
blocks
or use
answers
rather than
returns
answers
takes a lambda where you can put some logic to determine the value you want to be returned according to different conditions
and it also has some useful convenience methods like
firstArg()
to capture the arguments that were passed to the function being stubbed
i

ivano

04/08/2021, 3:06 PM
Thanks
m

Matteo Mirk

04/09/2021, 6:21 AM
If I may add a thought, this case hints that you don’t need a mock here, but just a stub or a “fake object” to be precise. You can either do as Mattia suggested, or you could write a fake implementation of
EmployeesRepositoryInterface
that applies your stub logic:
Copy code
class FakeEmployeesRepository : EmployeesRepositoryInterface {
  override fun getEmployee(id: String) {
    // calculate an employee by id...
  }
}
i

ivano

04/09/2021, 6:26 AM
thanks Matteo. well the old stub vs mock schools. Like it.
m

Matteo Mirk

04/09/2021, 6:29 AM
hehe yes, just pointing out that maybe in this case the use of mockk seems overkill to my eyes… just a personal taste. For the rest, go with mocks full steam ahead! (actually no, don’t go mock everything 😆 )
i

ivano

04/10/2021, 12:18 PM
and never becoming too much “relaxed” about mockks 😂
fantastic tool anyway, with superb synthax
🏆 1
❤️ 1
2 Views