I'm sorry I beat this drum again, but I searched and searched for a unit test way of rendering visualization in ASP.NET MVC (currently using v2).
I'm not 100% satisfied with using WatiN or Selenium for this, they are both great tools, but they take too long a test for what a simple script is and check the path much more than I need.
I am also deeply dissatisfied with the fact that the mantras "Opinions should not be checked", which, apparently, stem from the main reason for Views, in their current state, are simply not checked outside the larger integration test. :)
I already have a test on the controller with AssertViewRendered (). For ("Index"). WithViewData () "etc. I just want to say that the data is displayed in the view when it is turned on. Model.
Imagine this simple scenario:
controller:
public class SimpleController : Controller { public void Index() { var vm = new SimpleViewModel { Message = "Hello world!" }; return View(vm); } }
And this simple view model:
public class SimpleViewModel { public string Message { get; set; } }
And a simple view:
`<%@ Page Language="C#"` `Inherits="System.Web.Mvc.ViewPage<SimpleViewModel>" %>` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <h1><%= Model.Message %></h1> </body> </html>
How can I automate with a simple unit test that the view really uses the Message property, without the need for powerful integration testing tools like WatiN, and without a web server?
Something like this would be ideal:
[TestMethod] public void ShouldDisplayMessage() { const string helloWorld = "Hello world!"; var view = new SimpleView(new SimpleViewModel { Message = helloWorld }); var result = view.GetRenderedString(); Assert.IsTrue(result.Contains(helloWorld)); }
c # unit-testing asp.net-mvc
Jenk
source share