【发布时间】:2016-08-19 06:30:13
【问题描述】:
目前,我正在为使用组件的类编写集成测试。由于该组件使用第三方服务(在我的情况下为 AWS S3),因此我想用模拟组件替换该组件,以避免与第三方服务进行任何通信。
部分控制器类:
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' => 'john@doe.com',
'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 不存在的错误,我不确定我是否在正确的轨道上解决这个特定问题。因此,我的问题是如何将模拟/存根组件注入控制器并控制其行为(通过为方法设置固定的返回值)?
【问题讨论】:
标签: cakephp phpunit integration-testing cakephp-3.0