【问题标题】:How can I PHPUnit test a Controller_Plugin that uses a Redirector helper to redirect?我如何 PHPUnit 测试使用 Redirector 助手重定向的 Controller_Plugin?
【发布时间】:2012-05-22 07:49:09
【问题描述】:

我无法解决这个问题。

    $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

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

有什么想法吗?

【问题讨论】:

    标签: php zend-framework phpunit


    【解决方案1】:

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

    我认为这两行有问题。

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

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

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      相关资源
      最近更新 更多