O'Reilly logo

CakePHP 2 Application Cookbook by Jorge González, James Watts

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Stub method configuration

Mock objects have the ability to customize the return values for specific methods. Instead of returning null by default, you can configure the mock object methods to return a specific value, something that you'll find useful in your unit tests to check for specific edge cases when calling other objects (dependencies).

These also have the ability to define expectations on the methods called, to check if the right methods were called with the expected parameters or if they were called in the right order, and so on. This way, we don't need to actually test an external dependent class, but we can define a contract between the tested object and the dependencies.

Mock object methods can be configured to act as stub methods:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required