Using phpunit mock object, I have a method that returns an object.
How do you code this using the expects / method / will methods?
i.e.
->will($this->returnValue('Class_Name'));
Create the object, and return it with the returnValue()
function. For example:
$myObject = new RandomObject();
$myFactory = $this->getMock('ObjectFactory', array('getRandomObject'));
$myFactory->expects($this->any())->method('getRandomObject')->will($this->returnValue($myObject);
$this->assertInstanceOf('RandomObject', $myFactory->getRandomObject());
This will pass.
You can also create that object as a mock itself and pass the mock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With