为什么PHPUnit不将此函数视为已运行? [英] Why isn't PHPUnit counting this function as having ran?
问题描述
我的单元测试中有这样的代码:
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();
这就是说,即使functionThatCallsFoo
& foo
从内部进行var_dumping.
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 )
编辑
我更改了expects
调用以匹配直接从functionThatCallsFoo
调用的方法,但该方法仍然无法正常工作.
EDIT
I changed my expects
call to match the method directly called from functionThatCallsFoo
and it still did not work.
推荐答案
我要回答这个问题,因为我的代码存在很多问题.希望如果将来有人遇到类似问题,这个答案将成为一个清单.
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.
- 我的最终方法是静态的,因此我需要使用staticExpects而不是期望
- 我的静态调用使用self ::,但我需要使用static::( PHP> = 5.3)
- static ::不能用于私有函数,与self :: 不同
最后,我现在了解为什么静态函数是邪恶的.
In the end, I can now see why static functions are evil.
这篇关于为什么PHPUnit不将此函数视为已运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!