Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a variable within a mock object

Is there any way to set a class level variable within a mock object?

I have the mock object set similar to this:

$stub = $this->getMock('SokmeClass', array('method'));
$stub->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1));

Win the real class there is a variable that needs to be set for it to work properly. How can I set that variable within a mock object?

like image 487
Enrique Avatar asked Mar 09 '10 16:03

Enrique


People also ask

Can we mock a variable in python?

With a module variable you can can either set the value directly or use mock.


2 Answers

Here is what works for me:

$stub = $this->getMock('SomeClass');
$stub->myvar = "Value";
like image 116
vvkatwss vvkatwss Avatar answered Oct 11 '22 02:10

vvkatwss vvkatwss


Don't know why this works but it seems to for me. If you put the __get magic method as one of the overridden methods e.g.

$mock = $this->getMock('Mail', array('__get'));

You can then you can successfully do

$mock->transport = 'smtp';
like image 40
Nat Avatar answered Oct 11 '22 03:10

Nat