Zend_Test - 在控制器插件中为 PHPUnit 设置重定向 [英] Zend_Test - Setting redirect in Controller Plugin for PHPUnit

查看:34
本文介绍了Zend_Test - 在控制器插件中为 PHPUnit 设置重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 PHPUnit 来测试应用程序.我一切正常,但无法测试重定向.

I have been trying to use PHPUnit to test an application. I have it all working, but cannot test redirects.

我的重定向发生在 Acl 控制器插件内,而不是控制器中的操作内.

My redirects are occurring inside an Acl Controller Plugin, not inside an Action in a Controller.

我已将它们更改为使用建议的格式

I have changed them to use the suggested format of

$r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$r->gotoSimple("index", "index", "default");

但这在测试中失败了,响应体是空的,我得到了像

But this fails in the tests, the response body is empty and I get errors like

Zend_Dom_Exception: Cannot query; no document registered

如果我随后更改测试以使调度方法不会导致 gotoSimple() 被调用,那么测试将正确运行.

If I then change the test so that the dispatch method does not result in gotoSimple() being called then the test runs correctly.

我应该如何在我的应用程序中进行重定向,以便它与 Zend_Test 的响应对象一起正确运行?

How am I supposed to do a redirect in my application so that it runs correctly with Zend_Test's response object?

Zend 文档在大约两行中对此进行了介绍,我已经尝试过但失败了.

The Zend docs cover this in about two lines, which I have tried and it fails.

谢谢.

推荐答案

要测试是否发生了重定向,需要添加

To test that redirect has occurred, you need to add

$this->assertRedirectTo( 'index' );

运行后$this->dispatch();

您无法查询响应正文,因为在重定向的情况下它是空的(这是您的异常的来源).
您可以随时检查响应的实际样子

You cannot query the response body, since it's empty in case of redirect (that's where your exception comes from).
You can always check what the response actually looks like with

print_r( $this->getResponse() );

这篇关于Zend_Test - 在控制器插件中为 PHPUnit 设置重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆