How can I unit test or integration test for 404 - not found?

My team writes web hosting for content management in ASP.Net MVC 2 with S # arp architecture architecture. I use SpecFlow and WatiN for integration testing, and NUnit for unit testing.

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

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

I am new to TDD and BDD, so I may miss something obvious. In addition, I am doing retro testing in this project, which makes it a lot more difficult.

Thanks in advance.

+4
source share
1 answer

Usually, when we write BDD scripts, we write them from the user's point of view.

If the user is an ordinary person, they probably don’t care if the title is genuine 404. They would prefer the page to give them a clear and helpful message. Write a script to test for a 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 is not really about testing. It's about conversations that allow you to discover other things that you have not thought about, and develop a common understanding of what is about to happen. For example, what happens when a regular user tries to access the admin page? Do they have to be denied access or just don’t know what the page is? What if the page is deleted? These discussions are more useful than trying to attach everything.

If your 404 is associated with a specific message with the user, you can simply conduct testing to match the corresponding response. This will greatly reduce the likelihood of accidentally sending the wrong code with a message in the future, and you can focus on the real benefit.

+8
source

Source: https://habr.com/ru/post/1337024/


All Articles