unit-testing - Cakephp 2.3 单元测试参数 ['requested' ]

标签 unit-testing cakephp-2.3

我正在尝试测试一个只能从这样的元素调用的 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/

相关文章:

cakePHP saveMany 不工作

mysql - CakePHP 2.5 通过可包含的键进行复杂的查找过滤

cakephp - PHPStorm 自动完成 View 文件中的 CakePHP 自定义帮助程序

java - 使用 @InjectMocks 注入(inject) String 属性

JavaScript 单元测试或端到端测试

c# - Microsoft.VisualStudio.TestTools.UnitTesting 给出错误

mysql - CakePHP 并不总是返回外来字符

php - Mysql:插入后选择找不到刚插入的行

python - 当 Pyunit 和 Nose 测试失败时,为什么 py.test 通过单元测试(如预期的那样)?

unit-testing - PHPUnit 忽略注释