简体   繁体   English

如何使用asp.net mvc和单元测试来组织代码和测试

[英]How to organize code and tests with asp.net mvc and unit testing

So, I'm biting the bullet and trying to get started with asp.net MVC, unit testing and TDD. 所以,我咬紧牙关,试图开始使用asp.net MVC,单元测试和TDD。

I have a vague understanding of the concepts involved, which is to say somewhat beyond the "Hello World" level, but still pretty green. 我对所涉及的概念有一个模糊的理解,也就是说有点超出“Hello World”级别,但仍然很绿。 I'm ready for the rubber to meet the road, but I've found myself staring at the "New Project" dialog in VS for the last half hour... how, exactly, do you organize your unit tests? 我已经准备好迎接橡胶路了,但我发现自己在过去的半小时里盯着VS的“新项目”对话框...... 你究竟如何组织你的单元测试?

I see that with the standard VS Unit Test project type, it creates a separate project for the unit tests. 我看到,使用标准的VS Unit Test项目类型,它为单元测试创​​建了一个单独的项目。 Is that how I should proceed when using NUnit? 这是我在使用NUnit时应该如何进行的? Or should the tests be placed in the same project as the code being tested? 或者,测试应该与正在测试的代码放在同一个项目中吗?

None of the "getting started with unit testing..." type tutorials I've found seem to address this. 我发现的“单元测试入门......”类型教程似乎都没有解决这个问题。

Tests should be maintained in separate projects because you don't want to deploy testing code to a production environment. 测试应该在单独的项目中维护,因为您不希望将测试代码部署到生产环境。 For a single-project solution, one test project is probably sufficient. 对于单项目解决方案,一个测试项目可能就足够了。

Internally, a test project can be organized in any way you find convenient, as long as it's consistent. 在内部,测试项目可以以您认为方便的任何方式进行组织,只要它是一致的。 An ASP.NET MVC test project might have a ControllerTests folder with one test .cs file per controller, mirroring the MVC project structure to some degree. ASP.NET MVC测试项目可能有一个ControllerTests文件夹,每个控制器有一个测试.cs文件,在某种程度上镜像MVC项目结构。 This makes the tests easy to find and relate to the code they're testing. 这使得测试易于查找并与他们正在测试的代码相关。

how, exactly, do you organize your unit tests? 你究竟如何组织单元测试?

I organize my unit tests to reflect my project structure. 我组织单元测试以反映我的项目结构。 So fir example if in my ASP.NET MVC project I have 如果在我的ASP.NET MVC项目中我有这样的例子

  • Controllers 控制器
  • Models 楷模
  • Mappers 映射器
  • Validators ... 验证者......

I have those same folders in my unit test project. 我的单元测试项目中有相同的文件夹。 Then for each file in the MVC project I have a corresponding unit test. 然后对于MVC项目中的每个文件,我都有相应的单元测试。 Here's an example of a sample project structure I wrote . 这是我写的一个示例项目结构示例

My format has usually been like so: 我的格式通常是这样的:

MyMvcApp.Web (The Actual Web Application)
  |- Controllers
  |- ViewModels
  |- Views
  |- Framework (For specific override points in the MVC Framework)
MyMvcApp (The class library that contains my domain specific logic)
  |- SomeFacet (Folder to contain entities, objects, etc)
   |- Repositories
MyMvcApp.UnitTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)
MyMvcApp.IntegrationTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)

It is more important to understand first that what do we need to test and what facilities are available for the purpose. 首先要了解我们需要测试什么以及可用于此目的的设施更为重要。 It is basically the behaviour of the objects in the application you need to focus on. 它基本上是您需要关注的应用程序中对象的行为。 You have plenty of testing frameworks to choose from like NUnit Etc. Work on the structure the Test project as you carry on along with. 您有大量的测试框架可供选择,如NUnit等。随着您继续使用Test项目的结构。

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

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