简体   繁体   English

如何在asp.net-mvc单元测试中生成视图?

[英]How can generate views in asp.net-mvc unit tests?

Im trying to generate views in unit tests but i can't get around the missing VirtualPathProvider. 我试图在单元测试中生成视图,但我无法绕过丢失的VirtualPathProvider。 Most viewengines use the VirtualPathProviderViewEngine base class that gets the provider from the current HostingEnvironment. 大多数viewengines使用VirtualPathProviderViewEngine基类从当前的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. 在单元测试中没有HostingEnvironment,即使我创建了一个HostingEnvironment,也没有当前的VirtualPathProvider。

How can i workaround this problem? 我该如何解决这个问题? Do i have to create a custom FakeWebFormViewEngine? 我是否必须创建自定义FakeWebFormViewEngine?

There are features coming in VS Team System 2010 for the Acceptance Testing which would be appropriate for what you are trying to do. VS Team System 2010中有一些功能可用于验收测试,这些功能适用于您尝试执行的操作。 As mentioned by Gregory A Beamer Unit tests for MVC are done to the controller. 如Gregory A Beamer Unit所述,MVC的测试是对控制器进行的。 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. 有些人将模型视为业务实体,我将其视为特定于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之外测试我的DAL,业务逻辑层。 MVC really is all part of the presentation layer. MVC确实是表示层的一部分。 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. 至于视图,用户或通过像Watin这样的自动化测试它们。

I tried to do this as well. 我也尝试过这样做。 Unfortunately, it is not just the VirtualPathProvider (VPP) that is the problem. 不幸的是,问题不仅仅是VirtualPathProvider(VPP)。 The VPP is used to map the view or partial view to a physical path to determine the existance of the file. VPP用于将视图或部分视图映射到物理路径以确定文件的存在。 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. 遗憾的是,ViewContext以虚拟路径而不是物理路径结束,因此在渲染视图时,Builder使用不存在的HostingEvnironment属性。

If you are using a version of Visual Studio with Testing, then you could use a Web Unit Test. 如果您使用的是带有测试的Visual Studio版本,则可以使用Web单元测试。 This will allow you to use the browser to call the URL and then parse the response to check for values. 这将允许您使用浏览器调用URL,然后解析响应以检查值。

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. 在正确设置的ASP.NET MVC应用程序中,需要测试的代码位于控制器及其下方。 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? 我错过了什么吗?

您可以尝试Ivonna进行集成(以及在某种程度上,单元)测试您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM