Make sure there is a view

I am currently studying unit testing for a new application that I need to create. My main testing is going well (testing ActionResult classes is pretty nice). However, I want to make sure that there is a view in my solution. I am not 100% sure, my test is correct, therefore, if someone had suggestions, please feel free!

This is a test that I should verify that my login method to my security controller is working correctly:

[TestMethod] public void Login() { var authProvider = new Mock<IAuthenticationProvider>(); var controller = new SecurityController(authProvider.Object); var result = controller.Login() as ViewResult; Assert.IsNotNull(result, "ActionResult should be of type ViewResult."); Assert.AreEqual(result.ViewName, "login", "Does not render login page."); } 

My test explanation:

  • call the login method on the controller
  • Confirm its visualization of the view (by checking if it returns a ViewResult object)
  • Confirm that it displays the correct view (by checking the name of the view)

What I would like to have is the third statement to see if a view is actually rendered.

Some minor questions:

  • Should I beat this test?
  • Should I rename it (e.g. err, LoginRendersCorrectView or something else)

Thank!




Note. I am clearly trying to avoid file system validation. I kind of hope for a way to use ViewEngine to confirm that the view really exists.

+7
unit-testing asp.net-mvc
Jan 30 '09 at 0:40
source share
5 answers
  • No, I don’t think you should split the test until its just the third one, and not much more code.

  • Yes, I think a more useful name would be useful.

  • Since you have already verified that it has the correct name for the species, would it not be easy to make sure it is present?

I think it works great on full testing, but here I feel there might be a more efficient use of your time if you could go to the part where you check that the units that perform the actual specific input functions (such as checking password hashes or something else) works correctly.

+9
Feb 01 '09 at 0:58
source share

Knowing that representation exists in a solution is not very helpful. What you really need is that the view will be deployed, as your users (I hope) do not run your site in Visual Studio. In other words, what you are asking for is not a unit test, but an integration test. Therefore, you must use the appropriate tool for the job. Consider the structure of web testing, for example, Selenium .

+5
Feb 05 '09 at 14:12
source share

I totally agree with Jason, but I don’t think that what you are trying to do is really conducive to testing. In the end, behavior rendering and testing will already cover whether it exists or not.

Many developers go overboard when they are first bitten by a test development bug. They want the test crashes to tell them exactly what’s wrong, so they don’t need to dig and debug. This is not the main purpose of testing; testing is a test of the correct behavior, so you do not submit bad software. When something is wrong, you can debug. There is no need to have test wiring so specific that the test engine knows exactly what the problem is.

+4
Feb 04 '09 at 19:34
source share

You can try to use the FindView method of the ViewEngineCollection object that you have in the ViewResult to check if the MVC environment can find the view.

As I said, this 3rd Assert (which actually exists) is not something that will add real value to your tests, but, nevertheless, here is the code to check for existence:

 var viewEngineResult = result.ViewEngineCollection.FindView(controller, result.ViewName, result.MasterName); if (viewEngineResult == null) ... not found ... 

Hope this helps.

+1
Feb 05 '09 at 23:46
source share

If you are using a beta version, your code file will create a class for the presentation, which you can check for using reflection.

Otherwise, you can check if the file exists in the right place.

0
Jan 30 '09 at 6:46
source share



All Articles