News Feed
Sections




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

Federico Cargnelutti:
TDD Checking the return value of a Stub
April 16, 2014 @ 10:25:15

Federico Cargnelutti has a helpful post to his site today for the unit testing/TDD crowd about checking the retuned value from a stub of an object in your tests. He's using the built-in mocking framework here, not something like Mockery.

State verification is used to ensure that after a method is run, the returned value of the SUT is as expected. Of course, you may need to use Stubs on a test double or a real object to tell the object to return a value in response to a given message. [...] In PHP, for example, you dynamically type the return value within the body of the method. This means that PHP mocking libraries cannot check the type of the return value and provide guarantees about what is being verified. This leads to the awkward situation where a refactoring may change the SUT behaviour and leave a stub broken but with passing tests.

He gives an example of a few classes - a Presenter and Collaborator - and a test that mocks out the Collaborator instance, calling a "getStories" method on it. He shows a situation where all tests pass in the initial version, but after some changes to the return type, a test that should fail doesn't. His solution for the issue revolves around DocBlock annotations and the Return Value instead of the built-in mock object return method.

0 comments voice your opinion now!
tdd unittest return value stub passing test returnvalue mock

Link: http://blog.fedecarg.com/2014/04/15/checking-the-return-value-of-a-stub/

blog comments powered by Disqus

Similar Posts

SitePoint PHP Blog: pTest: PHP Unit Tester in 9 Lines Of Code

Chris Hartjes' Blog: Simple API Testing Using Behat

Tiffany Brown's Blog: PHP Quickie: More on is_numeric vs. ctype_digit

BiGiTSS Blog: Upgrading to PHP 5.3..! - are you sure?

NetTuts.com: Refactoring Legacy Code: Part 6 - Attacking Complex Methods


Community Events





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


library laravel zendserver framework symfony bugfix opinion series release language install package introduction interview voicesoftheelephpant deployment api podcast community unittest

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