【发布时间】:2012-04-13 11:00:32
【问题描述】:
这是我的测试:
public function testIncludeNumComment() {
$post = array(...stuff.....);
$result = $this->Comments->includeNumComments($post);
echo "end"; //this is not printed
$expected =1;
$this->assertEquals($result, $expected);
}
那么,我的控制器功能就是这个:
public function includeNumComments($post){
echo "printed";
$comments = $this->Comment->getNumComments($post['Post']['id']);
echo "not printed";
return $comments;
}
如您所见,控制器上对模型函数的调用不起作用
$this->Comment->getNumComments($idPost);
更重要的是,当我在 Comment 模型中的 getNumComments 函数的最开始引入 echo "hi"; 时,它也不会打印出来。 这就像它没有找到功能或类似的东西。 (但在测试过程中没有按屏幕显示任何错误)
它停在那里,不再执行更多代码。 我完全确定该函数运行良好,它只是从帖子中返回 cmets 的数量。问题是:为什么它在测试用例上不起作用?
谢谢。
更新: 测试设置如下所示:
public function setUp() {
parent::setUp();
$this->Comments = new TestCommentsController();
$this->Comments->constructClasses();
}
【问题讨论】:
-
核心 (>=2.0) 现在默认隐藏输出。但它还包含一种再次为您的测试启用调试输出的方法。您需要将 &debug=1 附加到测试套件中的 url(dereuromark.de/2011/12/04/unit-testing-tips-for-2-0-and-phpunit 提到了另一种解决方案)。
-
我现在已经完成了。我没有看到任何变化。它仍然没有显示模型上的 getNumComments 函数内的打印。
-
错字?为什么
$this->Comments->而不是正确的$this->Comment->? -
Cake Bake 命令创建了这样的测试。 CommentsController、Comment 模型和 Comments 视图是我的结构。
-
我刚刚为您更新了问题,以查看测试用例中的 setUp 是如何定义的。
标签: cakephp testing model controller cakephp-2.0