【发布时间】:2016-02-24 16:25:55
【问题描述】:
在我的应用程序代码中,我有一个 method_exists 检查以授权在创建过程中进行一些挂钩:
// Note: $myClass is implementing a ListItemFactory interface.
if ($isCreate) {
$methodName = "create{$attr}ListItem";
if (method_exists($myClass, $methodName)) {
$item = $myClass->$methodName();
} else {
[...]
}
}
我正在尝试测试这段代码,模拟$myClass 并检查$methodName 是否确实被调用。以下是我编写测试的方式:
/** @test */
function specific_create_method_is_called()
{
$factory = Mockery::mock(ListItemFactory::class)->makePartial();
$factory->shouldReceive("createCommentsListItem")->once();
[...]
}
但这不起作用,因为method_exists 没有在模拟中定义。我对模拟东西还很陌生,所以也许有一种明显的方法来管理这个问题,比如“存根”想要的功能,但我找不到方法......
提前感谢您的帮助。
【问题讨论】:
-
ListItemFactory 的源代码很值得一看。
-
只是解决了非常相似的问题,看看stackoverflow.com/questions/37927273/…这可能对你有帮助
标签: php unit-testing testing mockery