这是我的测试:
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”; 时,它也不会打印出来。 就像它没有找到功能或类似的东西。 (但在测试期间屏幕不显示任何错误)
它停在那里,它不会执行更多的代码。 我完全确定该函数运行良好,它只返回帖子中的注释数量。问题是:为什么它不适用于测试用例?
感谢。
更新: 测试setUp如下所示:
public function setUp() {
parent::setUp();
$this->Comments = new TestCommentsController();
$this->Comments->constructClasses();
}
答案 0 :(得分:0)
$result = $this->Comments->includeNumComments($post);
如果您的模型是“评论”,这没有多大意义
所以将其改为:
$result = $this->Comment->includeNumComments($post);
答案 1 :(得分:0)
检查您是否包含以下TestCommentsController
:
App::uses('TestCommentsController', 'Controller');
然后在beforeFilter
中编写AppController
函数,如下所示:
class AppController extends Controller {
......
...
public function beforeFilter() {
// you stuff
}
}
然后将此添加到包含test
功能的控制器:
public function beforeFilter(){
parent::beforeFilter();
$this->Comments = new TestCommentsController();
$this->Comments->constructClasses();
}
.....
现在继续......
public function testIncludeNumComment() {
$post = array(...stuff.....);
$result = $this->Comments->includeNumComments($post);
echo "end"; //this is not printed
$expected =1;
$this->assertEquals($result, $expected);
}