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.
unit-testing asp.net-mvc
Erik van Brakel Jan 30 '09 at 0:40 2009-01-30 00:40
source share