I'm currently looking into unit testing for a new application I have to create. I've got the basic testing going nicely (testing the ActionResult classes is pretty nice). One thing I do want to make sure though, is that a viewpage exists in my solution. I'm not 100% sure my test is correct, so if anyone had suggestions, please don't hesitate!
This is a test I have to check that my login method on my security controller is doing the right thing:
[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 explanation of the test would be:
- call the method 'Login' on the controller
- Confirm it's rendering a view (by checking if it returns a ViewResult object)
- Confirm it's rendering the right view (by checking the viewname)
What I would like to have is a third assert, to see if the view to be rendered actually exists.
Some secondary questions I have would be:
- Should I split this test up?
- Should I rename it (like, err, LoginRendersCorrectView or something)
Thanks!
Note: I'm explicitly trying to avoid having to check the filesystem. I'm sort of hoping for a way to use the ViewEngine to confirm the view actually exists.