News Feed
Sections




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

Change Garcia's Blog:
expected exceptions annotations, mocked object calls, oh my.
December 28, 2010 @ 11:42:27

Chance Gracia came across an interesting feature/bug when writing up unit tests for his application with PHPUnit. He was throwing an exception from a mock object and based on his annotation the exception is caught no matter the type.

Anyone who has worked with PHPUnit has most likely worked with expected exceptions and mock objects. The nice thing about working with expected exceptions is that we have access to a handy @expectedException annotation. I've gotten into the habit of using this for exceptions my fixtures should throw but also for when I'm using a mock object to verify a method call.

Some sample code is included to show his problem - a mock object is created for the "foo" class and a test to see if an Exception type is thrown is included on it. The problem is that the exception thrown isn't of type Exception, but of type "foo_exception" and the @expectedException annotation tells it to look for the Exception type. Because of this unpredictable behavior, he suggests not using annotations for these sort of tests or it could lead to false positives.

1 comment voice your opinion now!
annotation phpunit unittest exception custom expectedexception


blog comments powered by Disqus

Similar Posts

Ryan Mauger's Blog: Controller Testing with the FlashMessenger in Zend Framework

Lars Tesmer's Blog: How to Unit Test a Class Making Calls to an URL (or the Filesystem) With PHPUnit

PHPBuilder.com: Add Custom Components to Your PHP Development with PRADO

PHPMaster.com: Bulletproofing Database Interactions with PHPUnit's Database Extension

Content with Style: Getting phpunit to work with MAMP


Community Events

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


release unittest opinion security threedevsandamaybe community framework introduction interview extension api version podcast laravel5 language symfony laravel voicesoftheelephpant library series

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