9951 explained code solutions for 126 technologies

phpunitHow to create a mock object in PHPUnit?

Mock objects are used in unit testing to replace real objects. PHPUnit provides a framework for creating mock objects using the getMock() method.

Example code

$mock = $this->getMock('MyClass', array('methodName'));

This code creates a mock object of the class MyClass and allows the methodName to be called.

Code explanation

  • getMock(): This is the method used to create a mock object.
  • MyClass: This is the class that the mock object is based on.
  • methodName: This is the name of the method that can be called on the mock object.

Helpful links

Edit this code on GitHub