Hi guys and gals! Have a silly question, but would...
# mockk
a
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
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
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
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
Nice! Thats a great explanation of use-cases
c
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
well i actually agree 🙂
c
even better!
a
What makes it exotic?
because of Spy movies?
c
yeah like a james bond movie
a
Thats makes sense to me
c
the main point of mock testing is to isolate the dependencies
👍 1
m
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
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
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
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
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
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
just reconsider your design if its becoming hard to mock
m
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
TLDR: avoid logic in methods thad do IO, and avoid io in methods that have a lot of logic
👏 2
d
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.