测试重定向CakePHP 2.0 [英] Testing redirections CakePHP 2.0

查看:187
本文介绍了测试重定向CakePHP 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看看一些例子,但我没有得到它:
http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example

I have been looking at some examples at the cookbook but i dont get it: http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example

如何在像这样的删除操作中测试重定向?

How can i test a redirection in a delete action like this one?

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));        
    }
}

测试在重定向调用后停止, 't even print this echo:

The test stops after the redirect call, so it doesn't even print this echo:

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


推荐答案

测试您的删除操作相对于测试任何其他动作相同。您的测试用例可能类似这样。

Testing your delete action should be relatively the same as testing any other action. Your test case might look something like this.

// 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());
    }

}

当然,明显。您还可以检查会话并编写一个期望异常的测试。如果仍未达到测试用例的结束或继续,正在进行其他操作

Of course, this just checks the obvious. You can also check the session and write a test that expects the exception. If it's still not reaching the end of the test case or continuing on, something else is going on.

您可以使用在 ControllerTestCase 上的生成方法。

You can generate easy mocks by using the generate method on 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。

The above would first generate a mock of the PostsController to use during testing. It also mocks the send() method on the EmailComponent, and the entire SessionComponent.

有关嘲笑的更多信息: http://www.phpunit.de/manual/3.0/en/mock-objects.html

For more information on mocking: http://www.phpunit.de/manual/3.0/en/mock-objects.html

有关 generate() http://book.cakephp.org/2.0/en/development/testing.html#using-mocks-with- testaction

这篇关于测试重定向CakePHP 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆