1
目前,我正在为使用组件的类编写集成测试。由于此组件使用第三方服务(在本例中为AWS S3),我想用模拟组件替换组件,以避免与第三方服务进行任何通信。控制器类的将模拟组件注入控制器 - 集成测试 - cakephp 3
部分:
class AlbumsController extends AppController{
public $components = ['Aws', 'Upload'];
// Example of function that uses component
public function add(){
$album->pictures = $this->Aws->transformLinkIntoPresignedUrl($album->pictures);
}
}
集成测试的部分:
public function controllerSpy($event){
parent::controllerSpy($event);
if (isset($this->_controller)) {
$this->_controller->Auth->setUser([
'id' => $this->userId,
'username' => 'testtesttesttest',
'email' => '[email protected]',
'first_name' => 'Mark',
'last_name' => 'van der Laan',
'uuid' => 'wepoewoweo-ew-ewewpoeopw',
'sign_in_count' => 1,
'current_sign_in_ip' => '127.0.0.1',
'active' => true
]);
// If the component is set, inject a mock
if($this->_controller->Aws){
$component = $this->getMock('App\Controller\Component\AwsComponent');
$component->expects($this->once())
->method('transformLinkIntoPresignedUrl')
->will($this->returnValue(['link']));
$this->_controller->Aws = $component;
}
}
}
由于此抛出transformLinkIntoPresignedUrl不存在的错误,我不知道如果我在这个特定问题的正确轨道。因此,我的问题是,如何将模拟/存根组件注入控制器并控制其行为(通过设置方法的固定返回值)?