2013年7月17日星期三

James Morris: PHPUnit Mocking and Method Chaining


James Morris has an interesting new post about mocking and method chaining and a discovery he made about the proper use of the "at" method in what his mock objects were expecting.



I've been given the task of unit testing Symfony2's security layer, which at first seems daunting, but in reality with a clever bit of PHPUnit mocking, it's actually quite simple. Symfony2 makes heavy use of method chaining.


He illustrates one way to create the mocks for this chain (one mock returning another) but suggests an alternative - returning an instance of "self" to keep the chain alive. He also includes a bit about the "at" matcher and how, despite what the PHPUnit documentation says, it should be correctly used to handle the response of certain methods in the chained call.


Link: http://blog.jmoz.co.uk/phpunit-mocking-and-method-chaining

没有评论:

发表评论