PHPUnit和Zend Framework assertRedirectTo()问题 [英] PHPUnit and Zend Framework assertRedirectTo() issue

查看:78
本文介绍了PHPUnit和Zend Framework assertRedirectTo()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我创建的测试中,assertRedirectTo()出现问题,以下是我使用的代码:

I'm having an issue with assertRedirectTo() in a test I have created, below is the code I have used:

public function testLoggedInIndexAction() {
  $this->dispatch('/');
  $this->assertController('index');
  $this->resetResponse();
  $this->request->setPost(array(
   'type' => 'login',
   'username' => 'root',
   'password' => 'asdasd',
  ));
  $this->request->setMethod('POST');
  $this->dispatch('/');
  $this->assertRedirectTo('/feed/');
}

您通过/(index.php/)登录并在那里提交帖子的详细信息,它会将您重定向到/feed/(index.php/feed/).我提供的详细信息是正确的,应该可以使用,但是我遇到问题,PHPUnit表示它们不正确:

You log in through / (index.php/) and submit post details there and the it redirects you to /feed/ (index.php/feed/). The details I have supplied are correct and should work however I am having issues whereby PHPUnit is saying they are incorrect:

There was 1 failure:

1) IndexControllerTest::testLoggedInIndexAction
Failed asserting response redirects to "/feed/"

/home/public_html/mashhr/library/Zend/Test/PHPUnit/Constraint/Redirect.php:190
/home/public_html/mashhr/library/Zend/Test/PHPUnit/ControllerTestCase.php:701
/home/public_html/mashhr/tests/application/controllers/UserControllerTest.php:36

推荐答案

@poelinca:不,这只是Zend_Test在注册重定向时不可靠的情况(即使已正确调用!)

@poelinca: No, it is simply a case of Zend_Test being unreliable in registering a redirect (even if it has been called correctly!)

在他的情况下,真正的应用程序无疑会正确地重定向用户,但是Zend_Test环境在注册正确的重定向时遇到了麻烦.我能想到的最好的响应是忽略在应用程序中实际起作用的所有失败的assertRedirect.

In his case, the real app is no doubt redirecting the user properly, but the Zend_Test environment is having trouble registering properly called redirects. The best response I can think of is to omit any failing assertRedirect which actually works in the application.

这不是最佳情况,但是除非您准备深入研究Zend代码以查看问题出在哪里,否则这可能是提高效率的最佳选择.这是一个导致单元测试取一个不好的名字的例子:必须更改代码才能通过实际上已经有效的测试.

This is not an optimal situation, but unless you're prepared to dig into the Zend code to see where the problem is, this may be your best bet for efficiency. This is an example of what causes unit testing to get a bad name: Having to alter code to pass tests which actually work already.

请参见 http://framework.zend.com/issues/browse/ZF-7496 在标题中有一个误导性的具体含义:问题与所有重定向有关,尤其是那些必须设置标头并退出而不是分派原始控制器的重定向.

See http://framework.zend.com/issues/browse/ZF-7496 Which is misleadingly specific in its title: the problem relates to all redirects, especially those which must set headers and exit instead of dispatching the original controller.

无论出于何种原因,此行为都会导致重定向并非总是失败,而是非常不可靠!如果有人知道解决此问题的更好方法(这很普遍,并且可能与OP的代码无关),请告诉我们.

For whatever reason, this behavior causes Redirects not to always fail, but to be highly unreliable instead! If anyone knows a better workaround to this problem (which is general, and probably unrelated to the OP's code) please let us know.

这篇关于PHPUnit和Zend Framework assertRedirectTo()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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