【发布时间】:2012-04-30 20:15:42
【问题描述】:
我一直在看食谱上的一些例子,但我不明白: 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);
}
【问题讨论】:
-
我想知道它是否抛出了一个空视图错误。在这种情况下,您可以尝试在控制器上模拟
render函数,因为无论如何都没有删除视图。 -
嘲笑它是什么意思?是的,没有,重定向工作正常,视图操作不为空。
-
Mocking 是一个测试过程,你可以让某些方法返回你想要的结果。例如,
CakeRequest::send()被嘲笑并被告知什么也不做,因此它不发送标头。您甚至可以告诉模拟方法期望什么或如何响应(请参阅:phpunit.de/manual/3.0/en/mock-objects.html)。要使用 Cake 轻松模拟,请查看:book.cakephp.org/2.0/en/development/…。ControllerTestCase::testAction()会自动为您完成其中的一些操作。 -
我一直在尝试,但我没有得到它的工作。做这样的事情不是更简单吗? stackoverflow.com/questions/1986907/…
-
这可能会显着改变代码的行为。我会在一分钟内整理出一个正确的答案。
标签: unit-testing cakephp testing redirect cakephp-2.0