2016-10-07 18 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();