testAction()函数在CakePHP测试中的debug()上返回null [英] testAction() function returns null on debug() in CakePhp testing
问题描述
我试图学习如何在CakePhp中使用单元测试,我试图写一个控制器测试。我读了关于testAction()和debug()函数,但它不工作,我的意思是,测试方法通过,但是debug()返回null(因为testAction)
I was trying to learn how to use unit testing in CakePhp, I'm trying to write a controller test. I read about testAction() and debug() function, but It doesn't work for me, I mean, the test method passes, but the debug() returns null (because testAction does)
这是我的代码:
<?php
App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('PostsController', 'Controller');
class PostsControllerTest extends ControllerTestCase {
public function setUp() {
parent::setUp();
$Controller = new Controller();
$View = new View($Controller);
$this->Posts = new PostsController($View);
}
public function testIndex() {
$result = $this->testAction('Posts/Index');
debug($result);
}
}
帖子/索引控制器返回列表的所有帖子存储在DB中。
Posts/index controller returns a list of all posts stored in the DB.
推荐答案
我假设您使用CakePHP 2。
I'm assuming you're using CakePHP 2.
$ this-> testAction()
可以返回几个不同的结果,具体取决于您给出的选项。
$this->testAction()
can return a few different results, depending on the options you give it.
例如,如果将 return
选项设置为 vars
,则 testAction
方法将返回在测试操作中设置的vars数组:
For example, if you set the return
option to vars
, the testAction()
method will return an array of the vars that have been set in the tested action:
public function testIndex() {
$result = $this->testAction('/posts/index', array('return' => 'vars'));
debug($result);
}
在此示例中,调试数据应该是vars数组设置在 / posts / index
操作中。
In this example, the debug data should be an array of the vars that you set in the /posts/index
action.
CakePHP文档描述了您可以返回的可能结果: http://book.cakephp.org/ 2.0 / en / development / testing.html#selecting-the-return-type
The CakePHP documentation describes the possible results that you can have returned here: http://book.cakephp.org/2.0/en/development/testing.html#choosing-the-return-type
请注意,默认选项 result
,为您提供控制器操作返回的值。对于大多数控制器操作,这将是 null
,所以在你的例子中你得到 null
的事实是预期。
Note that the default option, result
, gives you the value that your controller action returns. For most controller actions this will be null
, so the fact that you're getting null
in your example is to be expected.
这篇关于testAction()函数在CakePHP测试中的debug()上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!