unit testing asp mvc view
How can i unit test the view of an ASP MVC application?
I have tried the mvc contrib test helper...
_controller.Index().AssertViewRendered();
but this doesn't actually test the view.
for example i 开发者_StackOverflow中文版can happily insert some bogus code in the view, and get the dreaded yellow screen of death, without my unit test ever knowing about it.
Is there any estabilished method of doing this? Do i need a mock out the view engine? Does mvccontrib cater to this?
I would have thought this would be a very common requirement, but i can't find much about it!
Thanks
EDIT What i'm really after is compile time checking, to ensure that model changes don't impact the view.
this question contained the instructions to enable build time view compilation which is sufficient for me for now. Compile Views in ASP.NET MVC
There are 3 options:
- You want to unit test the code in the view. In this case, you have to move the code to the controller, because it's not the responsibility of the view to have this code.
- You want to be sure the view is actually shown in the browser. Use an browser UI testing tool like waitin or selenium. This does not create an isolated test of the view, but of large parts of your application. That sounds like an advantage, but is there any value in an isolated test of a view?
- You want to test that code in your view is compilable code. Then compile the code. This can be done in a unit test, by manually calling view.compile or by turning on the view compiler in the build process.
It is possible to test your views via unit test using Visual Studio Test Tools, but only the values, created by the controller (e.g. values in ViewBag: ViewBag.message = "My message."
) or the name of the rendered view:
[TestMethod]
public void MyActionTest()
{
// Arrange
var lController = new HomeController();
// Act
var lResult = lController.MyAction() as ViewResult;
// Assert
Assert.IsTrue(lResult.ViewBag.message == "My message.", "Wrong Message in Viewbag.");
Assert.IsTrue(lResult.ViewName == "MyView", "Incorrect view.");
}
If you want to auto-test your entire view inclunding HTML, I recommend Selenium IDE as fast and simple solution and Selenium Web Driver for cross-browser-tests and experts.
Unit tests normally never test UI because it's simply too brittle to do so.
While you could argue that a minum test would be that the View doesn't crash with an exception when we attempt to render it, this would also be about the only unit test we could actually write for a View (ASP.NET MVC, WPF, Windows Forms, etc. - it doesn't really matter).
The next thing you (or your customer) would want to test is that the View is rendered correctly, and you can't reliably do this with an automated test. What it all boils down to is that Views are better tested by Visual Inspection because the return of investment on that is simply better than trying to develop and maintain automated UI tests.
You should look at the web testing tools in the MS Test suite or one of a number of other web testing tools, like Selenium to test the views, if you need automated tests for these. I think you'll find it easier than adapting a unit testing framework.
Full disclosure: I still test my UI by hand. I haven't found enough benefit to outweigh the cost of learning, setting up, and maintaining web tests.
My advice is to not bother to extensively test your views. It is hard, and If you are doing things right, there will not be much logic in them anyway.
That said, WatiN is a good tool for automated browser testing - not exactly what you wanted, but it works well.
It's fairly easy to test the view in an ASP.NET MVC page.
I walk you through step-by-step in a YouTube video. Here's an outline of the required steps:
You need to create a unit test in the controller. This allows to to easily call the rendered result. For example:
public ActionResult TestScenario() { // setup some models return View("Page", model); }
Your unit test needs to call that ControllerAction, and verify the result. In ApprovalTests this would be:
MvcApprovals.VerifyMvcPage(new MyController().TestScenario);
This is all rather straight-forward in ApprovalTests (www.approvaltests.com or nuget).
精彩评论