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 に答える