News Feed
Jobs 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

Trevor Morris' Blog: fuelphp, forms and validation

Zend Developer Zone: Working with RAR, LZF and BZ2 Compression Formats in PHP

Dutch Web Alliance: The definitive remote debug and unittest with PHPStorm guide

DevShed: Developing a Form Validation System with the Observer Pattern in PHP

php|architect: Bing Powered 404 for non-WordPress websites


Community Events











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


performance composer release security symfony2 component install hhvm application package language facebook introduction unittest database hack opinion support podcast framework

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