News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Chris Hartjes' Blog:
Metatesting Understanding Mock Objects
March 28, 2012 @ 08:19:20

In this new post to his blog Chris Hartjes gets into some details about some complex mocking he recently had to do in a project for work. He includes code snippets to illustrate.

With such an extensive array of tests [at work], I have received an education in what it really means to write unit tests for live, production-ready code that really takes unit testing seriously. [...] If you are really writing your unit tests the way you should, each test is focusing on testing one bit of functionality in isolation, which means that you will be heavily relying on mock objects to make things work. I've come up with an example scenario that I hope goes a long way to explaining how to effectively use mock objects.

In his example, he shows how to mock out two objects, "Foo" and "Bar" that are passed into a method in his "Alpha" class. He looks at the code for the "Foo" class and creates a mock object based on its contents - two methods: "getId" and "getDetails". He does the same with the "Bar" class, mocking the "getStartDate" and "getEndDate" methods and showing how to pass those into the "munge" method on the "Alpha" class, complete with return values to match the tests.

For more information on PHPUnit's mock object support, see this page in its manual. You might also explore another popular option, Mockery.

0 comments voice your opinion now!
mock object unittest phpunit class tutorial


blog comments powered by Disqus

Similar Posts

Ross Tuck: Persisting Value Objects in Doctrine

NetTuts.com: Creating an API-Centric Web Application

Richard Davey's Blog: Interesting memory use with GD images

Developer Drive: Creating a PHP User Survey: Writing to Database Tables

Mpwebwizard.com: Use a mirror for hits from popular websites


Community Events





Don't see your event here?
Let us know!


install podcast zendserver update api deployment framework symfony library release tips community series opinion laravel package interview language list introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework