0

这是我的测试:

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函数的一开始,它也不会被打印出来。这就像它没有找到功能或类似的东西。(但在测试期间它不会通过屏幕显示任何错误)

它停在那里并且不执行更多代码。我完全确定该函数运行良好,它只返回帖子中的评论数。问题是:为什么它不适用于测试用例?

谢谢。

更新:测试设置如下所示:

public function setUp() {
    parent::setUp();

    $this->Comments = new TestCommentsController();
    $this->Comments->constructClasses();

}
4

2 回答 2

0
$result = $this->Comments->includeNumComments($post);

如果您的模型是“评论”,这没有多大意义

所以将其更改为:

$result = $this->Comment->includeNumComments($post);
于 2012-04-13T11:35:34.953 回答
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);
  }
于 2012-04-13T17:07:15.840 回答