Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

zend framework - Returns 302 error in phpunit when the site redirection works fine

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

Failed asserting that 302 matches expected 200

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.

What should I have to change?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...