Why isn't PHPUnit counting this function as having ran?

本秂侑毒 提交于 2019-12-25 05:07:58

问题


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.

  1. My final method was static so I needed to use staticExpects instead of expects
  2. My static calls were using self:: but I needed to use static:: ( PHP >= 5.3 )
  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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!