如何 PHPUnit 测试使用重定向器帮助程序重定向的Controller_Plugin


How can I PHPUnit test a Controller_Plugin that uses a Redirector helper to redirect?

我无法解决这个问题。

    $this->_request->setBasePath('http://localhost/');
    $this->_request->setModuleKey('admin');
    $this->_request->setControllerKey('controller-page');
    $this->_request->setActionKey('index');
    $this->_sslRedirect->preDispatch($this->_request);
    $this->assertRedirectRegex('/https:/');

在我的控制器插件中,preDispatch() 可能会也可能不会重定向到 SSL。这在浏览器中有效,但是我无法为其编写测试。测试失败,如下所示:

Failed asserting response redirects to URL MATCHING "/https:/"
/srv/app/zend/library/Zend/Test/PHPUnit/Constraint/Redirect.php:190
/srv/app/zend/library/Zend/Test/PHPUnit/ControllerTestCase.php:764
/srv/app/www/tests/lib/Saffron/Controller/Plugin/SslRedirectTest.php:36
/usr/bin/phpunit:46

我还尝试在 preDispatch() 行之后立即在Zend_Test_PHPUnit_ControllerTestCase内使用 $this->dispatch(),但由于某种原因,这导致了错误 404 未找到...

有什么想法吗?

如果这是您的单元测试代码,我没有看到任何模拟,并且我完全确定我理解为什么以这种方式实现它(如果目标是强制 https 连接)

我认为这两行有问题。

$this->_sslRedirect->preDispatch($this->_request);
$this->assertRedirectRegex('/https:/');

如果您可以发布您正在测试的完整操作,以及您用于测试它的完整单元测试,这将有所帮助。