CakePHP3:集成测试中的模拟方法?

CakePHP3: Mock methods in integration tests?

我是单元/集成测试的新手,我想对我的控制器进行集成测试,它看起来像这样简化:

// ItemsController.php
public function edit() {

    // some edited item
    $itemEntity

    // some keywords
    $keywordEntities = [keyword1, keyword2, ...]

    // save item entity
    if (!$this->Items->save($itemEntity)) {
        // do some error handling
    }

    // add/replace item's keywords 
    if (!$this->Items->Keywords->replaceLinks($itemEntity, $keywordEntities)) {
       // do some error handling
    }
}

我有模型 ItemsKeywords,其中 Items 属于 ToMany 关键字。我想测试控制器的错误处理部分。所以我必须模拟 save()replaceLinks() 方法,它们将 return false.

我的集成测试是这样的:

// ItemsControllerTest.php
public function testEdit() {

    // mock save method
    $model = $this->getMockForModel('Items', ['save']);
    $model->expects($this->any())->method('save')->will($this->returnValue(false));

    // call the edit method of the controller and do some assertions...

}

这对 save() 方法来说工作正常。但它不适用于 replaceLinks() 方法。显然是因为它不是模型的一部分。

我也试过这样的方法:

$method = $this->getMockBuilder(BelongsToMany::class)
    ->setConstructorArgs([
        'Keywords', [
            'foreignKey' => 'item_id',
            'targetForeignKey' => 'keyword_id',
            'joinTable' => 'items_keywords'
        ]
    ])
    ->setMethods(['replaceLinks'])
    ->getMock();

$method->expects($this->any())->method('replaceLinks')->will($this->returnValue(false));

但这也行不通。模拟 replaceLinks() 方法有什么提示吗?

在做控制器测试时,我通常尽量少模拟,如果我想测试控制器中的错误处理,我会尝试触发实际错误,例如通过提供失败的数据 application/validation规则。如果这是一个可行的选择,那么您可能想尝试一下。

也就是说,模拟关联的方法应该按照您的示例中所示的方式工作,但您还需要用模拟替换实际的关联对象,因为与模型不同,关联没有全局注册表可以在其中放置模拟(这就是 getMockForModel() 将为您做的),以便您的应用程序代码无需进一步干预即可使用它们。

应该这样做:

$KeywordsAssociationMock = $this
    ->getMockBuilder(BelongsToMany::class) /* ... */;

$associations = $this
    ->getTableLocator()
    ->get('Items')
    ->associations();

$associations->add('Keywords', $KeywordsAssociationMock);

这将修改 table 注册表中的 Items table 对象,并替换(关联集合的 add() 更像 setter,即它会覆盖) 它与模拟对象的实际 Keywords 关联。如果你将它与模拟 Items 一起使用,那么你必须确保事先创建了 Items 模拟,否则上面示例中检索到的 table 不会被模拟一个!