我正在测试CakePHP中的控制器操作,根据特定条件重定向到不同的URL:
if ( !$userIsAdmin ) {
$this->Session->setFlash(__('Not allowed.'));
$this->redirect('/users');
}
if ( $this->MyModel->delete($id) ) {
$this->Session->setFlash(__('Success.'));
$this->redirect('/users');
}
else {
$this->Session->setFlash(__('Failed. Please, try again.'));
$this->redirect("/my_models/edit/$id");
}
问题是redirect(...)
在进行单元测试时实际上并没有重定向,因为它是一个模拟方法。
因此,当我尝试断言$this->headers
与预期的网址进行比较时,它会失败,因为跟踪的最后一个网址是上次执行的redirect()
调用中的网址。
有什么办法可以避免吗?
谢谢!