Skip to content
Advertisement

Mockery: how does a passive partial mock differ from the default mock?

In the very last paragraphs of this (great) quick reference to Mockery, the author explains some behaviour modifiers for a mock which are not default, but may be useful. These include the makePartial() call and the shouldDeferMissing() call.

How do these differ from the default behaviour? When you create a mock (Mockery::mock('myClass')), and don’t add any method expectations, all method calls go to the parent (i.e. MyClass), as far as I can see…

Here’s the last section of the Mockery quick reference.

Mockery::mock(‘MyClass’)->makePartial()

also

Mockery::mock(‘MyClass’)->shouldDeferMissing()

Known as a Passive Partial Mock (not to be confused with real partial mock objects discussed later), this form of mock object will defer all methods not subject to an expectation to the parent class of the mock, i.e. MyClass. Whereas the previous shouldIgnoreMissing() returned null, this behaviour simply calls the parent’s matching method.

Advertisement

Answer

  • Full mock objects require that all method calls must be expected
  • Real partial mock objects will mock only the methods specified, and you cannot set expectations for non-mocked methods. If you didn’t specify a method when creating the mock, it will defer all calls to that method to the parent class.
  • Passive partial mocks will defer method calls without an expectation to the parent class of the mock.

The difference between the latter two is that for passive partial mocks you don’t need to specify which methods you are going to mock before setting an expectation. When you set an expectation, it will automatically create the mocked method for you.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement