News Feed
Sections




News Archive
feed this:

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

Inviqa techPortal:
PHP Test Doubles Patterns with Prophecy
July 23, 2013 @ 12:31:08

The Inviqa techPortal has a new post today from Marcello Duarte about using Prophecy in unit testing to work with mocks and doubles.

Test doubles are a way of replacing all classes in our tests other than the one we are testing. They give us control on direct and indirect outputs that would have been generated by those classes. Understanding the role of the test double helps us to write better tests, and thus better code. [...] When writing unit tests we are focusing on describing one unit of behaviour at a time, and how objects communicate in order to achieve that behaviour. We instantiate the object under test, call a method of the object and observe if the outcome is as expected.

In his examples he uses Prophecy to mock out different kinds of objects - a dummy object, a "fake" (with listeners for other object calls), a stub that mocks other internal method calls and a more familiar mock object. He also talks about two guidelines to follow when thinking about writing tests and code complexity - the Law of Demeter and "Don't Mock What You Don't Own." He gives an example of working around this last one by using the Adapter design pattern.

0 comments voice your opinion now!
prophecy unittest designpattern doubles mock stub dummy fake

Link: http://techportal.inviqa.com/2013/07/23/php-test-doubles-patterns-with-prophecy

NetTuts.com:
All About Mocking with PHPUnit
September 28, 2012 @ 09:46:21

NetTuts.com has another post for those out there wanting to further their PHP unit testing knowledge. In this new tutorial, Csaba Patkos introduces you to mocking objects with PHP Unit - a powerful method to help test some of the more difficult things.

There are two styles of testing: "black box" and "white box" styles. Black box testing focuses on the object's state; whereas, white box testing focuses on behavior. The two styles complement each other and can be combined to thoroughly test code. Mocking allows us to test behavior, and this tutorial combines the mocking concept with TDD to build an example class that uses several other components to achieve its goal.

He illustrates mocking with his sample "toy car" application and shows how to use a few different types of mocks (test doubles) to create some tests. These types include dummy objects, test stubs, test mocks and test fakes. Code is included for all test examples, including some showing the use of the actual PHPUnit mocking functionality.

0 comments voice your opinion now!
mocking phpunit tutorial doubles unittest



Community Events





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


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

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