为什么PHPUnit不将此函数视为已运行? [英] Why isn't PHPUnit counting this function as having ran?

查看:87
本文介绍了为什么PHPUnit不将此函数视为已运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单元测试中有这样的代码:

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.

  1. 我的最终方法是静态的,因此我需要使用staticExpects而不是期望
  2. 我的静态调用使用self ::,但我需要使用static::( PHP> = 5.3)
  3. static ::不能用于私有函数,与self ::
  4. 不同

最后,我现在了解为什么静态函数是邪恶的.

In the end, I can now see why static functions are evil.

这篇关于为什么PHPUnit不将此函数视为已运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆