× {{alert.msg}} Never ask again
Receive New Tutorials
GET IT FREE

PHP Testing Tutorial: Mocks, Doubles, Dummies and Stubs

– {{showDate(postTime)}}

Codementor PHP expert Chris Hartjes is a frequent conference speaker advocating testing, and he co-organized the GTA PHP User Group.

Chris recently joined us during Codementor’s Office Hours, and some of the users asked about testing doubles.

The text below is a summary done by the Codementor team and may vary from the original video and if you see any issues, please let us know!


Do you advocate a lot of mocks? If so, do you ever get the feeling that you aren’t actually testing anything? How do you get past this feeling?

Yes, I do use mocks. I believe mocks are a really good way to tell if your code is overly complex. If you’re having to do a lot of mocks in order to let a testing scenario work, chances are you have an object that you’re testing that is doing too much. Everyone is familiar with the “God” object idea, which has large classes with 20~30 methods on them. If you start looking at what the object is responsible for doing and how the methods are supposed to be manipulating the state of the object, you’ll quickly realize you have some design mistakes.

People talk about code smells. Having to do excessive mocking is a test smell. It means you have a test scenario that’s really complicated and the object you’re trying to test can’t do it without a lot of help. I do use mocks because mocks are the only way to test third-party things that aren’t under your control (e.g. database connections, calls to remote API) and puts them into a proper state as a dependency. Altogether, I mock anything I can’t control, and I try to look and see if there are ways to create a real version of a dependency with the test for things I can control. However, excessive mocking is a sign there is a problem with the code itself.

Can you talk about the differences between Doubles, Dummies, Stubs, Spies, Mocks & Fakes and when to use each one?

In the PHP community, these are all known as mocks because the tools says get mocks. Mockery is my favorite mock tool.

There are two types of test doubles the PHP community mostly uses: dummies and stubs. A dummy creates a mock of an object that just has to stand in because you need that type of object to exist as a dependency, not because you need it to do anything. You just need it to exist to satisfy a parameter check as it’s passed around. A stub creates a test double where I’ll tell it we have this test object that’s pretending to be another thing, I know that I’m expecting specific methods to be called, and when they are, I’m expecting specific results to come back.

Another type of test double people sometimes use is spies. The concept in testing and test doubles in spies is you’ll be trying to verify that a specific thing was run. PHP unit combines these things where if you’re creating a test double using a get mock builder tool, you can also specify that you’re expecting it to be run a specific number of times. Mockery changed that a little bit where it sometimes doesn’t care how many times things are run. I have most commonly used spies for work where I test for Javascript. The tools in Javascript’s own model help you do that a lot easier than you can in PHP. An example where spies would be interesting is if you’re passing in a collection of 5 items and want to make sure a method inside this loop that’s iterating the whole collection is called five times. You can’t really do that in PHP, but you can do it through a Javascript library.

Other posts in this series with Chris Hartjes:

Chris HartjesNeed Chris’s help? Book a 1-on-1 session!

View Chris’s Profile

or join us as an expert mentor!



Author
Chris Hartjes
Chris Hartjes
Dynamic Algol programmer since 1998, now promoting automated testing and code calisthenic practices
Hire the Author

Questions about this tutorial?  Get Live 1:1 help from PHP experts!
RajhaRajesuwari S
RajhaRajesuwari S
5.0
Full Stack PHP / NODE/REACT/ WORDPRESS/SHOPIFY web developer
I am an experienced full stack developer 15 years in the field with consistent knowledge in developing web portals with expertise in all opensource...
Hire this Expert
Humayun Shabbir
Humayun Shabbir
5.0
Expert Visual Basic, C# and JavaScript Developer | 3500+ sessions
Welcome to my profile on Codementor! I'm a dedicated full-time mentor with a track record of over 3500 sessions since 2015. My journey in...
Hire this Expert
comments powered by Disqus