12

I'm sorry to be beating this drum again, but I've been searching and searching for a way to unit test a rendered view in ASP.NET MVC (currently using v2).

I'm not 100% satisfied with using WatiN or Selenium to do this, they both are great tools, but take far too long to run a test for what is such a simple scenario, and are testing way way more than I need.

I am also deeply unsatisfied with the "Views should not be tested" mantra that seemingly stems from the root cause of Views, in their current state, just aren't testable outside of a larger integration test. :)

I've already got a test on the Controller with "AssertViewRendered().For("Index").WithViewData()" etc. I am simply wanting to cover that the data is displayed by the view when it is on the 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 do I automate with a simple unit test that the View actually uses the Message property, without needing to use heavy-weight 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));
    }
5
  • Considering that you view won't have any logic (which is recommended), is it worth creating and maintaining unit tests for views?
    – goenning
    Commented Mar 10, 2011 at 17:04
  • 2
    In a word.. Yes :) We want to have a test that shows we are using the properties of the ViewModel in the View. Given this is a very lightweight test, I don't want to have to use Selenium/WatiN (and a browser, and IIS, and the controller, and everything else needed to run and display a webpage) to do this. It takes too long :)
    – Jenk
    Commented Mar 10, 2011 at 17:54
  • I've been pushed along the road of 'don't test things you have no control over'. Which basically means, your time is probably wasted attempting to write tests which validate the framework is doing it's job. Sorry to re-iterate the point, but you should spend your time testing routines you've actually composed. Commented May 1, 2013 at 20:46
  • 3
    We have composed the view, though. I'm not asserting that the framework does it's job. I am asserting that the view consumes and displays a value passed to it. We have plenty of control over what is displayed by the view - hence why I want it tested.
    – Jenk
    Commented Jun 10, 2013 at 9:59
  • More than testing if the data is there I'd like to test if the data was processed as expected. I'm looking for a way to have access to the rendered html, so I could pass a mock for the model and assert for a specific structure of the html.
    – Jp_
    Commented Sep 19, 2017 at 19:18

1 Answer 1

1

This issue is that your View file contains other information that effects the view (aka the markup). You can test the view model to has the correct information in the view model but I'm not sure that's exactly what you want.

You can cast your ViewResult.ViewData.Model as you view model and assert values from there.

    [Test]
    public Test()
    {
        var homeController = new HomeController();
        var result = homeController.About() as ViewResult();
        Assert.IsInstanceOf(typeof(MyViewModel),result.ViewData.Model);
        var myModel = result.ViewData.Model as MyViewModel;
        Assert.That(myModel.Name,Is.EqualTo("Hello World")  );

    }

If you used the spark view engine things might be a little easier

http://darrell.mozingo.net/2010/01/28/in-memory-view-rendering-with-spark/

1
  • 1
    I've already tested this, as I explained in the original question :) Spark is something I have considered, but at this stage I do not want to have to consider a new view engine for the sake of a simple test. :)
    – Jenk
    Commented Mar 10, 2011 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.