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

Anton Afanasev

05/06/2021, 4:50 PM
Hi guys and gals! Have a silly question, but would like to get your opinion. How you decide whether use spyk or mockk? When you would prefer one on top of another?
m

Mattia Tommasone

05/06/2021, 4:53 PM
it depends 🙂 basically, a mockk has the features of both a spyk and a stub, meaning that you can use it in
every
blocks and in
verify
blocks a spyk can only be used in
verify
blocks and it will execute the real methods of the object it spies
a

Anton Afanasev

05/06/2021, 4:58 PM
Does that mean that I can say that: It is better to use
spyk
for any object that does not use an
every
? Since it is more close to being a real object?
m

Mattia Tommasone

05/06/2021, 4:59 PM
generally speaking, yes however, it’s not a universal rule 🙂 think of the case where you don’t want the actual function to be invoked but you are ok with a default behavior
you don’t need to use an
every
but you can use a relaxed mock
say for instance you have a
database.saveSomething()
function returning Unit you don’t want to really save things to database in this case so you don’t want to use a spy
but you can use a relaxed mock and not have to use
every
on it
a

Anton Afanasev

05/06/2021, 6:00 PM
Nice! Thats a great explanation of use-cases
c

christophsturm

05/06/2021, 6:08 PM
i know this contradicts a bit what @Mattia Tommasone said. but IMO most of the time you will want to use a mock. a spy is something much more exotic, and if you are not sure that you want a spy you probably don’t.
m

Mattia Tommasone

05/06/2021, 6:09 PM
well i actually agree 🙂
c

christophsturm

05/06/2021, 6:09 PM
even better!
a

Anton Afanasev

05/06/2021, 6:09 PM
What makes it exotic?
because of Spy movies?
c

christophsturm

05/06/2021, 6:10 PM
yeah like a james bond movie
a

Anton Afanasev

05/06/2021, 6:11 PM
Thats makes sense to me
c

christophsturm

05/06/2021, 6:11 PM
the main point of mock testing is to isolate the dependencies
👍 1
m

Mattia Tommasone

05/06/2021, 6:11 PM
thing is, IMO, in a test you really want to drive the behavior of the context around your SUT to make sure you only test your SUT
if you find out using a spy is the better option, then it may be a signal that your two objects are too tightly coupled
c

christophsturm

05/06/2021, 6:12 PM
i mean theres a reason why its called mockk
😂 1
spys are just easy to implement once you have a mock library so its added value
a

Anton Afanasev

05/06/2021, 6:13 PM
I was thinking that spy might be better because it use the real object for dependency and therefore its behaviour is more close to real life
But if the idea (and probably it is) to isolate SUT , then mock be doing better job.
I believe that main reason I asked this questions is because of various articles talking about that, if you find yourself mocking a lot - it is a sign of bad design.
c

christophsturm

05/06/2021, 6:15 PM
the most important thing in a unit test is that you always know why it fails and why it passes
and a spy just makes it less deterministic
m

Mattia Tommasone

05/06/2021, 6:16 PM
it is indeed true that if you find yourself mocking a lot it is a sign of bad design, but that’s also true if you find yourself spying a lot 🙂
👍 1
a

Anton Afanasev

05/06/2021, 6:18 PM
Does that mean that I should try and avoid mock based tests and use fakes-alike or it is not that critical?
@christophsturm, thanks for the link
c

christophsturm

05/06/2021, 6:19 PM
just reconsider your design if its becoming hard to mock
m

Mattia Tommasone

05/06/2021, 6:19 PM
my idea is that if you find yourself mocking a lot maybe your SUT is doing too many things
or maybe if the context around it is hard to mock there are some components that can be isolated better
c

christophsturm

05/06/2021, 6:20 PM
TLDR: avoid logic in methods thad do IO, and avoid io in methods that have a lot of logic
👏 2
d

dave08

05/18/2021, 9:39 AM
This is a nice little article to differentiate between types of test doubles: http://blog.cleancoder.com/uncle-bob/2014/05/14/TheLittleMocker.html
I usually tend to avoid mocks altogether, but I recently needed to assure that a repo fake's method wasn't called from the SUT, so spyk came in handy.
3 Views