2016-10-07 17 views
1

我试图模拟CakePHP 3组件检查用户是否被允许查看页面。尝试模拟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

什么是您的成分是什么样子? – jeremyharris

回答

0

指定嘲笑方法与setMethods()getMock()

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