How can I create views in asp.net-mvc unit tests?

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

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

Unit tests do not have HostingEnvironment, even if I create it, there is no current VirtualPathProvider.

How can I solve this problem? Do I need to create a custom FakeWebFormViewEngine?

+6
unit-testing asp.net-mvc
source share
4 answers

VS Team System 2010 has the features needed to conduct acceptance testing that are suitable for what you are trying to do. As mentioned by the Gregory A Beamer Unit, tests for MVC are performed by the controller. You can also test the model depending on how you implement your model.

There is a lot of controversy. Some people view the model as business objects, where I view them as representations of a model specific to the presentation. More viewing models. Since my model does not have real functionality, I do not need to test it. I am testing my DAL, Business Logic Layer outside of MVC. MVC is indeed part of the presentation layer. This breakdown of your Presentation is not for your application. You are still overlaying your application.

As for module testing, the controller is testing. You can test your model if there are methods that require testing. As for the views that they accept, tested by users or through automation, for example, Watin.

+2
source share

I also tried to do this. Unfortunately, the problem is not only in VirtualPathProvider (VPP). VPP is used to map a view or partial view to a physical path to determine the existence of a file. Unfortunately, the ViewContext ends in a virtual way, not a physical way, so when rendering a view, Builder uses HostingEvnironment properties that do not exist.

If you are using a version of Visual Studio with a test, you can use the Unit Test web interface. This will allow you to use a browser to call the URL, and then parse the response to check the values.

0
source share

Forgive me if this sounds ignorant, but what is the purpose of creating views? Maybe something is missing for me, but the main topic of unit tests is device testing. In a properly configured ASP.NET MVC application, the code to be tested is located in the controller and below. In fact, I would say if it is properly designed, it is lower.

Performance testing is a user acceptance test. I see nothing wrong with automating this by any means, but I'm not sure if this is what you need to do with the unit test.

Did I miss something?

0
source share

You can try Ivonna to integrate (and to some extent units) test your views.

0
source share

All Articles