Zend Framework 中的 UnitTest 错误控制器

发布于 2024-10-05 19:45:01 字数 887 浏览 2 评论 0原文

我喜欢 100% 代码覆盖率,但我不知道如何在 Zend Framework 中测试 ErrorController。

测试404Action和errorAction是没有问题的:

    public function testDispatchErrorAction()
    {
        $this->dispatch('/error/error');
        $this->assertResponseCode(200);
        $this->assertController('error');
        $this->assertAction('error');
    }

    public function testDispatch404()
    {
        $this->dispatch('/error/errorxxxxx');
        $this->assertResponseCode(404);
        $this->assertController('error');
        $this->assertAction('error');
    }

但是如何测试应用程序错误(500)呢? 也许我需要这样的东西?

public function testDispatch500()
{
    throw new Exception('test');

    $this->dispatch('/error/error');
    $this->assertResponseCode(500);
    $this->assertController('error');
    $this->assertAction('error');

}

Iam a fan of 100% code coverage, but i have no idea how to test the ErrorController in Zend Framework.

It is no problem to test the 404Action and the errorAction:

    public function testDispatchErrorAction()
    {
        $this->dispatch('/error/error');
        $this->assertResponseCode(200);
        $this->assertController('error');
        $this->assertAction('error');
    }

    public function testDispatch404()
    {
        $this->dispatch('/error/errorxxxxx');
        $this->assertResponseCode(404);
        $this->assertController('error');
        $this->assertAction('error');
    }

But how to test for an application error (500)?
maybe i need something like this?

public function testDispatch500()
{
    throw new Exception('test');

    $this->dispatch('/error/error');
    $this->assertResponseCode(500);
    $this->assertController('error');
    $this->assertAction('error');

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-10-12 19:45:01

这是一个老问题,但我今天一直在努力解决这个问题,在其他地方找不到好的答案,所以我将继续发布我为解决这个问题所做的事情。答案其实很简单。

将您的调度指向将导致抛出异常的操作。

当向 JSON 端点发出 get 请求时,我的应用程序会抛出错误,因此我使用其中之一来测试这一点。

   /**
   * @covers  ErrorController::errorAction
   */
    public function testErrorAction500() {
        /**
         * Requesting a page that doesn't exist returns the proper error message 
         */
        $this->dispatch('/my-json-controller/json-end-point');
        $body = $this->getResponse()->getBody();
        $this->assertResponseCode('500');
        $this->assertContains('Application error',$body);
    }

或者,如果您不介意仅用于测试的操作,则可以创建一个仅引发错误的操作并在单元测试中指向该操作。

public function errorAction() {
    throw new Exception('You should not be here');
}

那么你的测试将如下所示:

   /**
   * @covers  ErrorController::errorAction
   */
    public function testErrorAction500() {
        /**
         * Requesting a page that doesn't exist returns the proper error message 
         */
        $this->dispatch('/my-error-controller/error');
        $body = $this->getResponse()->getBody();
        $this->assertResponseCode('500');
        $this->assertContains('Application error',$body);
    }

This is an old question but I was struggling with this today and couldn't find a good answer anywhere else so I'll go ahead and post what I did to solve this problem. The answer is actually really simple.

Point your dispatch to an action that will result in a thrown exception.

My application throws an error when a get request is made to a JSON end point, so I used one of those to test this.

   /**
   * @covers  ErrorController::errorAction
   */
    public function testErrorAction500() {
        /**
         * Requesting a page that doesn't exist returns the proper error message 
         */
        $this->dispatch('/my-json-controller/json-end-point');
        $body = $this->getResponse()->getBody();
        $this->assertResponseCode('500');
        $this->assertContains('Application error',$body);
    }

Alternatively, if you don't mind having an action just for testing, you could just create an action that only throws an error and point to that action in your unit test.

public function errorAction() {
    throw new Exception('You should not be here');
}

Then your test would look like this:

   /**
   * @covers  ErrorController::errorAction
   */
    public function testErrorAction500() {
        /**
         * Requesting a page that doesn't exist returns the proper error message 
         */
        $this->dispatch('/my-error-controller/error');
        $body = $this->getResponse()->getBody();
        $this->assertResponseCode('500');
        $this->assertContains('Application error',$body);
    }
无声情话 2024-10-12 19:45:01

好吧,我对这个主题不太熟悉,但我会使用自定义 ErrorHandler 插件来操纵此行为(扩展原始版本,并假装抛出异常)。也许可以只注册一次测试。

Well, I'm not very familiar with this subject, but I'd manipulate this behavior with a custom ErrorHandler plugin (extend the original, and pretend that an exception was thrown). Maybe it's possible to register it only for one test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文