小编Phi*_*ppe的帖子

Mockery:如何在method_exists中使用shouldReceive?

在我的应用程序代码中,我有一个method_exists检查来授权在创建过程中挂钩:

// Note: $myClass is implementing a ListItemFactory interface.

if ($isCreate) {
  $methodName = "create{$attr}ListItem";

  if (method_exists($myClass, $methodName)) {
    $item = $myClass->$methodName();
  } else {
    [...]
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试此代码,模拟$myClass并检查是否$methodName实际上已调用.这是我写测试的方式:

/** @test */
function specific_create_method_is_called()
{
  $factory = Mockery::mock(ListItemFactory::class)->makePartial();
  $factory->shouldReceive("createCommentsListItem")->once();
  [...]
}
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,因为在mock中没有定义method_exists.我是一个很嘲笑的东西,所以也许有一个明显的方法可以解决这个问题,比如"捣乱"想要的功能,但是我找不到方法......

在此先感谢您的帮助.

php testing unit-testing mockery

7
推荐指数
1
解决办法
380
查看次数

标签 统计

mockery ×1

php ×1

testing ×1

unit-testing ×1