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

PHP 10.0 Blog: Kill resources

Chris Hartjes' Blog: Snakes and Elephants Playing Nice Together: PHPUnit and py.test with Hudson

Greg Beaver's Blog: PHP namespaces become much better (Derick take note)

Pieter's Blog: Zend_Form with XML Tutorial

Bogdan Albei's Blog: Quickstart web services with SOAP and Zend Framework


Community Events





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


list symfony deployment series laravel code conference zendserver tips introduction voicesoftheelephpant api release bugfix language interview podcast community threedevsandamaybe framework

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