The first topic in the #30daysoftesting challenge that I know very little about is today’s topic.
Compare and contrast mocking, stubbing and faking.
Since I take this #30daysoftesting challenge with the purpose of learning, it is OK that I know so little about some topics. I was happy to read on Twitter that others were not (very) familiar with it yet either.
So let’s break this post down into 3 parts to discuss these 3 different test doubles. All examples used are literally taken from above resource, pragmatists.com and more specifically the author Michal Lipski:
Mocking is asserting that a certain call was made to perform an action. For example ‘sending an email’. You assert that the specific e-mail call is made to send an email whenever you perform an action that is supposed to trigger that.
A stub is a pre-defined value (or a collection of pre-defined values) that is returned when you make a call to some functionality. Imagine you want to know the average grade of a student, you can hardcode the values 4, 8 and 9 (or just 1 of those), and then you always receive a pre-defined value that allows you to continue testing.
A fake is a fully functional object but a simplified version of the real object, for example: instead of checking something against a SQL database if a certain username exists, a fake checks against a collection in the code that contains some test usernames. I like to think of a fake as a ‘model’ of the real thing. Another example is a fake payment processor that always returns that a payment was done successfully. This way you can easily test the happy flow of your software when there is not yet a fully implemented payment processor available.
Altogether a very interesting topic and definitely good to know what options there are when it comes to test doubles.