网站重定向正常时,在phpunit中返回302错误 [英] Returns 302 error in phpunit when the site redirection works fine

查看:79
本文介绍了网站重定向正常时,在phpunit中返回302错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ZF2中使用PhpUnit进行单元测试时,会显示以下消息:

When doing unit testing using PhpUnit in ZF2, the following message shows up:

未能断言302与预期的200相匹配

Failed asserting that 302 matches expected 200

我了解它是由于ZF2的重定向命令而出现的:

I understand that it shown up because of the redirection command of ZF2:

return $this->redirect()->toUrl('/admin/album/add');

但是当我在浏览器中访问它时,该站点可以使用上面的代码进行查找.但是在单元测试中失败了.

But when I access it in the browser, the site works find with the above code. But it fails in the unit testing.

我该改变什么?

推荐答案

浏览器与单元测试不同,反之亦然.您的期望或结果有所不同.

A browser is not the same as unit testing or vice versa. Your expectations or results are different.

接收到302响应代码的浏览器将进行重定向,这意味着它将在响应中使用URL并提交新的请求.您在浏览器中看到的结果是重定向页面.

A browser that receives a 302 response code HAS to redirect which means it will take the URL in the response and submit a new request. The result you see in your browser is the redirected page.

单元测试不会重定向.您的单元测试只会执行您指示的操作.如果您的单元测试应该测试重定向,那么您可以评估响应,正确的断言是302而不是200.

Unit testing does not redirect. Your unit test is only doing what you direct it to do. If your unit test should test for the redirect then you evaluate the response and the correct assertion is 302 and not 200.

如果要确保重定向中的URL正确,则必须编写另一个测试,该测试实际上将获取新页面并测试该响应,结果为200 OK.

If you want to make sure that the URL in the redirect is correct you'll have to write another test that actually fetches the new page and test that response for 200 OK.

这篇关于网站重定向正常时,在phpunit中返回302错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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