在 Zend Redirector 操作助手中使用 set redirectUrl 方法
-
28-09-2020 - |
题
我正在 SimpleTest 中对 Zend_Controller_Action 的一些扩展进行单元测试。我希望能够使用 Redirector 操作助手的 set 方法设置重定向 url,然后使用 Redirector 的 redirectAndExit() 方法在稍后的过程中实际重定向。这个过程似乎没有像我通过阅读文档并查看操作控制器、重定向器和响应类中的代码所期望的那样工作。
这是我编写的 UnitTestCase 方法:
public function testSetGoToUrl() {
$request = new Zend_Controller_Request_Http();
$response = new Zend_Controller_Response_Http();
$controller = new App_Zend_Controller_Action($request, $response, array());
$controller->getHelper('redirector')->setGoToUrl('/');
}
App_Zend_Controller_Action 类只是抽象类 Zend_Controller_Action 的具体扩展。我在这里所做的只是实例化控制器并设置重定向 URL。正如预期的那样,SimpleTest 报告器首先发送标头。但是这个测试方法会生成“标头已发送”异常,我不明白为什么。我不知道在这种情况下调用了任何 run() 或dispatch() 方法。
发送第二组标头的是什么?
解决方案
在测试时你应该使用 Zend_Controller_Response_HttpTestCase
和 Zend_Controller_Request_HttpTestCase
.
Zend_Controller_Response_HttpTestCase::sendHeaders()
返回将发送的所有标头的数组,而不是执行实际的 header()
声明。
这些也用于 Zend_Test_PHPUnit_ControllerTestCase
.
不隶属于 StackOverflow