简体   繁体   English

如何将单元测试项目添加到现有MVC3应用程序(从空模板)

[英]How Can I Add a Unit Test Project to an Existing MVC3 Application (from empty template)

I created an MVC3 application from the Empty template so I couldn´t add a Visual Studio Unit test project to the solution. 我从Empty模板创建了一个MVC3应用程序,因此我无法在解决方案中添加Visual Studio Unit测试项目。

I made some changes, added some controllers and now I want to try TDD so I need to add a framework for testing. 我做了一些更改,添加了一些控制器,现在我想尝试TDD,所以我需要添加一个框架进行测试。

But I can´t see how I can do that. 但是我看不出我能做到这一点。

I want some way where I can create my test project right in the Solution Explorer, for example "website.Test" with some basic folders and files. 我想在某种方式,我可以在解决方案资源管理器中创建我的测试项目,例如“website.Test”与一些基本的文件夹和文件。

I saw here some questions about how to add Unit tests, but those were with xUnit.net or NUnit and I want the original VS test framework. 我在这里看到了一些关于如何添加单元测试的问题,但是那些是xUnit.net或NUnit,我想要原始的VS测试框架。

I am using Visual Studio 2010 Professional. 我正在使用Visual Studio 2010 Professional。

I am also interested to know what others think about the 3rd-party unit test frameworks, are they better? 我也有兴趣知道其他人对第三方单元测试框架的看法,他们更好吗?

What about for beginners? 初学者怎么样?

要添加空的测试项目,只需转到文件>添加>新建项目,选择测试子类别>测试项目。

The naming of the test-project is significant! 测试项目的命名非常重要! If your MVC-project is named "MyProject" your test-project has to be named "MyProject.Tests" in order to connect both of them. 如果您的MVC项目名为“MyProject”,则您的测试项目必须命名为“MyProject.Tests”才能连接它们。 Tools like MvcScaffolding will not work otherwise! 像MvcScaffolding这样的工具不会起作用!

File > Add > New Project > Test > Unit Test Project , which has name: "{ProjectName}. Tests ". File > Add > New Project > Test > Unit Test Project ,其名称为“{ProjectName}。 测试 ”。 Next step's to add References, best way's to open your {ProjectName} -> References and r-click on System.Web. 下一步是添加引用,最好的方法是打开你的{ProjectName} - >引用,然后在System.Web上单击r。 Mvc, look: Mvc,看:

在此输入图像描述

Then will open a new window, copy path to file: 然后会打开一个新窗口,复制路径到文件:

在此输入图像描述

And add your reference :-) to unit test project : 并将您的参考:-)添加到单元测试项目中

在此输入图像描述

Then select Browse and go to Dictionary (remember to delete filename, fe: g:\\disk\\yourdll.dll = g:\\disk\\ ): 然后选择Browse并转到Dictionary (记得删除filename,fe: g:\\disk\\yourdll.dll = g:\\disk\\ ):

在此输入图像描述

Next step's similar but add your {ProjectName}.dll by this way: 下一步是类似的,但通过这种方式添加您的{ProjectName} .dll:

{ProjectName}.Tests -> References -> Add and Add {ProjectName}.dll: {ProjectName}.Tests -> References -> Add and Add {ProjectName} .dll:

在此输入图像描述

Follow with presented steps and add references from your {ProjectName} to {ProjectName}.Tests. 按照提供的步骤添加{ProjectName}到{ProjectName} .Tests的引用。

Important dll: 重要的dll:

  • System.Web.Mvc System.Web.Mvc
  • System.Web.WebPages System.Web.WebPages
  • System.Web.WebPages.Deployment System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor System.Web.WebPages.Razor
  • System.Web.Razor System.Web.Razor

For your Solution do "File->New->Project". 对于您的解决方案,请执行“文件 - >新建 - >项目”。 Select "Test->Unit Test Project". 选择“测试 - >单元测试项目”。 Then for a name enter "YourSolution.Tests" and press OK. 然后为名称输入“YourSolution.Tests”并按OK。 Next add a reference to YourSolution: on "YourSolution.Tests->References" right click and browse to Solutions, make sure that there is a check box selected for YourSolution and press Ok. 接下来添加对YourSolution的引用:在“YourSolution.Tests-> References”上单击鼠标右键并浏览到“解决方案”,确保为YourSolution选中了一个复选框,然后按“确定”。

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

相关问题 单元测试您的mvc3应用程序的步骤? - steps to unit test your mvc3 application? 我如何单元测试Windsor可以解决Asp.net MVC3控制器依赖? - How can I unit test that Windsor can resolve Asp.net MVC3 controller dependencies? 如何在ASP.NET MVC3网站上对我的Json结果进行单元测试? - How can I unit test my Json result in an ASP.NET MVC3 web site? 如何将MVC 5项目模板添加到VS 2012? - How can I add the MVC 5 project template to VS 2012? MVC3-如何覆盖IEnumerable的Editor /模板/行为 - MVC3 - how can I override the template/behavior of EditorFor IEnumerable 如何对MVC UserViewControl进行单元测试? - How can I unit test an MVC UserViewControl? MVC3应用程序,在现有架构上 - MVC3 application, on existing architecture 如何在nUnit中对我的asp.net mvc应用程序的视图和授权属性进行单元测试? - How can I unit test views and authorization attributes of my asp.net mvc application in nUnit? 我需要一个WPF应用程序来填充HTML模板(理想情况下,就像MVC项目一样)。 我怎样才能做到这一点? - I need a WPF application to populate a HTML template (ideally like an MVC project can). How can I do this? 如何运行单元测试项目并将结果返回到外部应用程序? - How can I run a unit test project and return the results to an external application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM