【问题标题】:Error when trying to mock CakePHP 3 component尝试模拟 CakePHP 3 组件时出错
【发布时间】:2017-02-16 12:48:24
【问题描述】:

我正在尝试模拟一个 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

很可能我错误地创建了模拟。谁能告诉我如何正确操作?

【问题讨论】:

  • 你的组件是什么样的?

标签: unit-testing cakephp mocking phpunit


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2015-12-21
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2016-08-19
    相关资源
    最近更新 更多