× {{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!

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!
Foysal
Foysal
5.0
javascript enthusiast with a taste for all new web technologies.
I have been programming on various web and cross platform stacks for 5 years. I love making things and helping people build things. If you need...
Hire this Expert
Stefan Georg
Stefan Georg
5.0
Senior Full Stack Software Developer
I've been coding since age of 15, starting with perl, moving to php, and finally to python. I've been a freelancer working in the US, New Zealand,...
Hire this Expert

Or Become a Codementor!

Live 1:1 help from expert developers

Codementor is your live 1:1 expert mentor helping you in real time.

comments powered by Disqus
Codementor is your live 1:1 expert helping you in real time