CakePHP单元测试不返回内容或视图 [英] CakePHP Unit Test Not Returning Content or View

查看:200
本文介绍了CakePHP单元测试不返回内容或视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的项目实现测试用例,以包含在我们的构建脚本中。这个CakePHP 2.4应用程序主要是基于REST,并已设置为在其routes.php文件中解析json扩展。下面是我们的应用程序中典型函数的示例:

I'm working on implementing test cases for our project to be included in our build script. This CakePHP 2.4 application is largely REST based and has been set to parse json extensions in its routes.php file. Here is an example of a typical function in our application:

public function index() {
    $this->paginate = array(
        'fields' => array('User.username', 'User.first_name', 'User.last_name', 'UserGroup.name','Location.name','User.id'),
        'conditions' => array(
            'active'=>1
        ),
        'link' => array('UserGroup','Location')
    );
    $this->set('response', $this->RestApi->getResponse());
    $this->set('_serialize','response');
}

这将返回一个如下的json结构:

This would return a json structure like this:

{"sEcho":1,"iTotalRecords":16,"iTotalDisplayRecords":14,"aaData":[["admin","Administrative"],["salesman","Salesman"]]}

在调用此URL时,我会收到一个JSON响应,我的一个测试将是正确地解码它,并继续验证数据。问题是,对testAction的调用不返回任何东西(我曾试图告诉它返回视图和内容)。我能够访问响应的唯一方法是通过查看返回vars。

So I expected when writing my test cases that in calling this URL I would receive a JSON response, and one of my tests would be to properly decode it and move on with validating the data. Problem is, the call to testAction returns nothing (I'ved tried telling it to return view and contents). The only way I am able to access the response is by looking at the return vars.

示例 - 不工作:

$result = $this->testAction('/users/index.json',array(
    'method' => 'GET',
    'return' => 'contents'
));
var_dump($result); // NULL

示例 - 正在工作:

$result = $this->testAction('/users/index.json',array(
    'method' => 'GET',
    'return' => 'vars'
));

那么,看vars有什么问题呢?好了一个它比应用程序如何工作的一个真实世界的例子。这也让我有点担心架构,如果我不能得到一个基本单元测试工作按预期。

So what's the problem with looking at vars? Well for one its a little further than a real world example of how the application works. It also makes me a little worried about the architecture if I can't get a basic unit test to work as expected.

请指教和感谢您的阅读!

Please advise and thanks for reading!

编辑:我应该补充一点,即使对一个非rest url,如一个只返回HTML的testAction,我仍然没有得到任何响应。没有HTML等...

I should add that even when doing a testAction against a non rest url, such as one that just returns HTML, I still get no response. No HTML etc...

推荐答案

我最终解决了这个问题。有必要禁用Auth组件。有一些博客和讨论线程,如何在测试自己这样做,但似乎冗长乏味,必须重复相同的代码在每个测试。我的解决方案是在测试运行时自动禁用Auth组件。

I eventually solved this. It was necessary to disable the Auth component. There are some blogs and discussion threads on how to do this within the tests themselves, but it seemed tedious to have to repeat the same code in every test. My solution was to automatically disable the Auth component when tests ran.

在AppController :: beforeFilter中:

In AppController::beforeFilter:

// For Mock Objects and Debug >= 2 allow all (this is for PHPUnit Tests)
if(preg_match('/Mock_/',get_class($this)) && Configure::read('debug') >= 2){
    $this->Auth->allow();
}

我觉得这是非常优雅的,因为我们更关心部署的核心功能。我们可能会回去创建一个单元测试来验证Auth / ACL组件是否正常工作。

I felt this was pretty elegant since we care more about core functionality on deployments. We will likely go back and create a unit test to validate the Auth / ACL components are working as they are supposed to.

这篇关于CakePHP单元测试不返回内容或视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆