问题
I have code like this inside my unit test:
// $item_id was defined above
$originalMock = $this->getMock( 'Item', array( 'foo' ), array(
$item_id
));
$originalMock->expects( $this->once() )->method( 'foo' );
$originalMock->functionThatCallsFoo();
It is saying I'm not calling foo
at all, even though functionThatCallsFoo
& foo
are var_dumping out from within.
There are several function calls between the publicly called function and the one I'm expecting. I made sure there are no static functions called down the chain. ( There were originally but I changed them to see if I can get this working at all )
EDIT
I changed my expects
call to match the method directly called from functionThatCallsFoo
and it still did not work.
回答1:
I'm going to answer this question since I had a world of problems with my code. Hopefully this answer will be a bit of a checklist if someone else has similar problems in the future.
- My final method was static so I needed to use staticExpects instead of expects
- My static calls were using self:: but I needed to use static:: ( PHP >= 5.3 )
- static:: can't be used on a private function, unlike self::
In the end, I can now see why static functions are evil.
来源:https://stackoverflow.com/questions/10537100/why-isnt-phpunit-counting-this-function-as-having-ran