问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/13117088/returns-302-error-in-phpunit-when-the-site-redirection-works-fine