4

クックブックでいくつかの例を見てきましたが、わかりません: http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example

このような削除アクションでリダイレクトをテストするにはどうすればよいですか?

public function delete($id = null){         
        $this->Comment->id = $id;
        if (!$this->Comment->exists()) {
            throw new NotFoundException(__('Invalid comment'));
        }
        if ($this->Comment->delete()) {         
            $this->Session->setFlash(__('Comment deleted'));
            return $this->redirect(array('controller' => 'posts', 'action' => 'view', $idPost));
        }
        $this->Session->setFlash(__('Comment was not deleted'));
        return $this->redirect(array('controller' => 'posts', 'action' => 'view', $idPost));        
    }
}

リダイレクト呼び出しの後にテストが停止するため、このエコーも出力されません。

public function testDelete(){       
    $result = $this->testAction("/comments/delete/1");
    echo "this is not printed";
    print_r($this->headers);        
}
4

4 に答える 4

7

削除アクションのテストは、他のアクションのテストと比較的同じである必要があります。テストケースは次のようになります。

// notice it extends ControllerTestCase
class PostsControllerTest extends ControllerTestCase {

    function testDelete() {
      $this->testAction('/posts/delete/1');
      $results = $this->headers['Location'];
      // your OP code redirected them to a view, which I assume is wrong
      // because the item would be deleted
      $expected = '/posts/index';
      // check redirect
      $this->assertEquals($results, $expected);

      // check that it was deleted
      $this->Posts->Post->id = 1;
      $this->assertFalse($this->Posts->Post->exists());
    }

}

もちろん、これは明らかなことをチェックするだけです。セッションをチェックして、例外を予期するテストを作成することもできます。それでもテストケースの終わりに達していないか、続行していない場合は、別のことが起こっています。

generateのメソッドを使用して、簡単なモックを生成できますControllerTestCase

function testDelete() {
  $Posts = $this->generate('Posts', array(
    'components' => array(
      'Email' => array('send'),
      'Session'
    )
  ));
  // set ControllerTestCase to use this mock
  $this->controller = $Posts;

  $this->testAction('/posts/some_action_that_sends_email');
}

上記は、最初にテスト中に使用するPostsControllerのモックを生成します。また、EmailComponentのsend()メソッドとSessionComponent全体をモックします。

モックの詳細については、http ://www.phpunit.de/manual/3.0/en/mock-objects.htmlを参照してください。

詳細については、 http generate()//book.cakephp.org/2.0/en/development/testing.html#using-mocks-with-testactionをご覧ください。

于 2012-04-17T14:46:32.853 に答える
1

$idPostが定義されていないため、エラーが発生する可能性があります。

私は次のように書きます:

public function delete($id = null){         
        $this->Comment->id = $id;
        if (!$this->Comment->exists()) {
            throw new NotFoundException(__('Invalid comment'));
        }
        if ($this->Comment->delete()) {         
            $this->Session->setFlash(__('Comment deleted'));
        } else {
            $this->Session->setFlash(__('Comment was not deleted'));
        }
        $this->redirect(array('controller' => 'posts', 'action' => 'view', $id));        
    }
}

そして、次のようにテストします。

public function testDeleteWithSuccess() {
        $Controller = $this->generate('Comments', array(
            'components' => array(
                'Session'
            ),
            'models' => array(
                'Comment' => array('exists')
            )
        ));

        $Controller->Comment->expects($this->once())
            ->method('exists')
            ->will($this->returnValue(true));

        $Controller->Session->expects($this->once())
            ->method('setFlash')
            ->with('Comment deleted');

        $this->testAction("/comments/delete/ID");

        $this->assertEquals($this->headers['Location'], 'http://'. $_SERVER['HTTP_HOST'] . '/posts/view/ID');
    }
于 2012-04-30T04:38:30.317 に答える
0

エコーが出力されることはありません。関数「削除」は、終了する前に常にリダイレクトを呼び出します。

于 2012-04-17T11:39:40.813 に答える
0
public function delete($id = null){         
    $this->Comment->id = $id;
    if (!$this->Comment->exists()) {
        throw new NotFoundException(__('Invalid comment'));
    }
    if ($this->Comment->delete()) {         
        $this->Session->setFlash(__('Comment deleted'));
    } else {
        $this->Session->setFlash(__('Comment was not deleted'));
    }
    $this->redirect(array('controller' => 'posts', 'action' => 'view', $id));        
}

}

プレニクス、それは全然違うんじゃない?コメントを削除して、コメントの ID を投稿のビュー コントローラーに渡していますか? それで、あなたはそうすることで投稿またはコメントを表示していますか?? 他の提案はありますか?

于 2012-04-30T12:15:42.697 に答える