views:

333

answers:

3

Im trying to generate views in unit tests but i can't get around the missing VirtualPathProvider. Most viewengines use the VirtualPathProviderViewEngine base class that gets the provider from the current HostingEnvironment.

protected VirtualPathProvider VirtualPathProvider {
    get {
        if (_vpp == null) {
            _vpp = HostingEnvironment.VirtualPathProvider;
        }
        return _vpp;
    }
    set {
        _vpp = value;
    }
}

In unit tests there is no HostingEnvironment, even if i create one there is no current VirtualPathProvider.

How can i workaround this problem? Do i have to create a custom FakeWebFormViewEngine?

A: 

I tried to do this as well. Unfortunately, it is not just the VirtualPathProvider (VPP) that is the problem. The VPP is used to map the view or partial view to a physical path to determine the existance of the file. Unfortunately, the ViewContext ends up with the virtualpath, not the physical path, so when the view is rendered the Builder uses properties of the HostingEvnironment which does not exist.

If you are using a version of Visual Studio with Testing, then you could use a Web Unit Test. This will allow you to use the browser to call the URL and then parse the response to check for values.

Matthew
A: 

Pardon me if this sounds ignorant, but what is the purpose of generating views? I may be missing something, but the primary focus of unit tests is "testing the unit". In a properly set up ASP.NET MVC application, the code that needs to be tested is in the controller and below. In fact, I would say, if properly developed, it is below.

The test of the view is a user acceptance test. I see nothing wrong with automating this, by any means, but I am not sure this is something that has to be done with a unit test.

Am I missing something?

Gregory A Beamer
I agree, but there are some scenarios where a unit test is useful i think. There are view engines that allow "scripting" inside the views (spark), some simple tests for testing this functions would be great i think.Don't get me wrong, i don't want to compare generated HTML code, it's more like "is the login form visible?" stuff.
Franz P.
+2  A: 

There are features coming in VS Team System 2010 for the Acceptance Testing which would be appropriate for what you are trying to do. As mentioned by Gregory A Beamer Unit tests for MVC are done to the controller. You can also test the Model depending on how you implement your model.

This is where there is a lot of controversy. Some people look at the model as business entities where I look at them as representations of the model specific to the View. More of a View Model. Since there is no real functionality in my model I do not have to test it. I test my DAL, Business Logic Layer outside of the MVC. MVC really is all part of the presentation layer. It is layering of your Presentation not your application. You still layer your application.

As far as Unit testing goes the controller is where you test. You can test your model if there are methods that require testing. As for the views they are acceptance tested by users or through automation like Watin.

cjibo