Phpunit mock - return an object

Using phpunit mock object, I have a method that returns an object.

How do you code this using expects / method / will methods?

i.e.

->will($this->returnValue('Class_Name')); 
+4
source share
1 answer

Create an object and return it using the returnValue() function. For instance:

 $myObject = new RandomObject(); $myFactory = $this->getMock('ObjectFactory', array('getRandomObject')); $myFactory->expects($this->any())->method('getRandomObject')->will($this->returnValue($myObject); $this->assertInstanceOf('RandomObject', $myFactory->getRandomObject()); 

It will pass.

You can also create this object as the layout itself and pass the layout.

+8
source

All Articles