尝试模拟CakePHP 3组件时出错

时间:2016-10-07 13:36:14

标签: unit-testing cakephp mocking phpunit

我正在尝试模拟CakePHP 3组件,该组件检查是否允许用户查看该页面。

我试过了:

$authComponent = $this->getMockBuilder(App\Controller\Component\AuthorizationComponent::class)
    ->getMock();

$authComponent
    ->method('isAuthorized')
    ->willReturn($this->returnValue(true));

然而,在运行测试时,它说:

Trying to configure method "isAuthorized" which cannot be configured because it does not exist, has not been specified, is final, or is static

很可能我错误地创建了模拟。谁能告诉我如何正确地做到这一点?

1 个答案:

答案 0 :(得分:0)

setMethods()之前使用getMock()指定模拟方法:

$authComponent = $this
    ->getMockBuilder(App\Controller\Component\AuthorizationComponent::class)
    ->setMethods(['isAuthorized'])
    ->getMock();