Testing pages that don't work in Laravel
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
Ran into a little bit of trouble today trying to test that a page didn't work in my Laravel app. I wanted to ensure that a user who tried to access a page they weren't authorised to view did in fact not get access to that page. Unfortuantely, using visit()
as I normally do wasn't doing the trick.
This resulted in a failing test, because (as expected) the page didn't work. However, I had no good way of testing this. seeStatusCode()
and assertResponseStatus()
were not working with this arrangement. So I branched out, but it was ugly.
I couldn't settle on this, so I kept digging, and then found the obvious and simple solution. Don't use visit()
and just make a simple request to the page. That then gives you access to the two status code assertions I mentioned above.
Beautiful!