我正在尝试测试一个只能从这样的元素调用的 Controller 操作:
$notes = $this->requestAction(array(
'controller' => 'notes'
) , array(
'pass' => array(
'location' => $requestUrl
)
));
在操作本身上,会进行检查以确保该操作是“请求的”:
public function index() {
if (!empty($this->params['requested'])) {
...
return $notes;
} else {
throw new ForbiddenException();
}
}
如何测试上面的代码?以下内容:
$this->testAction('/notes', array(
'passed' => array('location'=>'location1'),
'return' => 'vars'
));
触发 ForbiddenException。我尝试使用 $this->generate 但我不确定应该如何生成 $this->controller->params 对象。
最佳答案
感谢@nanoman 的回复。然而,我找到了一个更简单的解决方案(不知道为什么我之前会想到这个)。写在这里以供将来引用。
只需从测试中调用 requestAction 即可!
function testMytest() {
$this->controller = $this->generate('Notes', array(
'components' => array(
'Auth'
)
));
$notes = $this->controller->requestAction(array(
'controller' => 'notes'
) , array(
'pass' => array(
'location' => 'location1'
)
));
}
关于unit-testing - Cakephp 2.3 单元测试参数 ['requested' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14865096/