Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unit test or integration test for 404 - Not Found?

My team is writing a content-managed web-hosting application in ASP.Net MVC 2 with the S#arp Architecture framework. I'm using SpecFlow and WatiN for integration testing, and NUnit for unit testing.

I have a custom controller factory that locates a page in the database based on the URL and site, and then loads the proper controller and action. It also loads an error controller when the page (or site) is not found in the database.

I want to write either a unit test or integration test that validates a 404 page is properly displayed when the URL is invalid. WatiN cannot check the response header, so it cannot accurately ensure an actual 404 page was loaded. This may eliminate an integration test as the solution.

I'm new to TDD and BDD, so I may be missing something obvious. Also, I am retro-fitting tests into this project, which makes it that much more difficult.

Thanks in advance.

like image 448
Eddie Avatar asked Feb 26 '23 07:02

Eddie


1 Answers

Normally when we write BDD scenarios, we write them from the user's perspective.

If the user is an ordinary person, they probably don't care too much about whether the header is a genuine 404. They'd prefer a page giving them a clear and useful message. Write the scenario to check for the clear and useful message.

Given no section on unicorns exists
When the user browses for horses
And changes the url to be about unicorns
Then the user should be told that no such page exists.

BDD isn't really about testing. It's about the conversations which let you discover other things you haven't thought of, and developing a common understanding of what should happen. For instance, what happens when an ordinary user tries to access an admin page? Should they get "access denied" or simply not know that the page is there? What if a page is deleted? Those discussions are more useful than trying to pin everything down.

If your 404 is associated with a particular message to the user, then you can simply unit-test that the appropriate response matches. That will drastically reduce the chances of accidentally sending the wrong code with a message in the future, and you can focus on the real benefit.

like image 61
Lunivore Avatar answered Mar 15 '23 04:03

Lunivore