简体   繁体   English

如何在ASP.NET MVC多层应用程序中正确进行单元测试?

[英]How to correctly do unit-tests in ASP.NET MVC multi-layer application?

I've used unit-tests in small ASP.NET MVC projects with monolithic architecture. 我在具有单片架构的小型ASP.NET MVC项目中使用了单元测试。 In this case I just had 2 projects like SampleApplicationName and SampleApplicationName.Tests . 在这种情况下,我只有2个项目,如SampleApplicationNameSampleApplicationName.Tests

Now I am working in a team of 3 developers on a big project with really nice n-layer architecture. 现在,我正在一个由3名开发人员组成的团队中,在一个非常好的n层架构的大项目上工作。 We have 5 projects in solution : 我们有5个项目在解决方案:

1.Data (POCO), 2.DataAccess (EF), 3.Common (helpers here), 4.Business, 5. UI (ASP.NET MVC5). 1.Data(POCO),2.DataAccess(EF),3.Common(这里是助手),4.Business,5.UI(ASP.NET MVC5)。

Here araise a question. 这里提出一个问题。 How should I manage tests? 我该如何管理测试? Should it be separate test-projects for each exist project like Business.Tests, DataAccess.Tests , or I need to test only the final result? 它应该是每个存在的项目(如Business.Tests,DataAccess.Tests)的单独测试项目,还是我只需要测试最终结果? I mean that each method runs full path from UI -> BLL -> DAL, so should I test each project separately or have only one test project? 我的意思是每个方法都从UI运行完整路径 - > BLL - > DAL,那么我应该单独测试每个项目还是只有一个测试项目?

Usually you'll end up with 通常你会最终得到

  • a separate unit test project for each project with logic inside (if you're just keeping POCOs in a project it might not deserve a dedicated UT project) that are testable (you might want to skip the DataAccess layer) 每个项目的单独的单元测试项目,其中包含逻辑内部(如果您只是将POCO保留在项目中,它可能不值得专用的UT项目)是可测试的(您可能希望跳过DataAccess层)
  • a couple of integration testing projects (UI & DataAccess layers might be good candidates here, although you might structure your IT projects in a different manner, by scenario - or even all together in a place) 几个集成测试项目(UI和DataAccess层可能是这里的好选择,尽管您可能以不同的方式,按场景构建您的IT项目 - 甚至可以在一个地方一起构建)

I mean that each method runs full path from UI -> BLL -> DAL, so should I test each project separately or have only one test project? 我的意思是每个方法都从UI运行完整路径 - > BLL - > DAL,那么我应该单独测试每个项目还是只有一个测试项目?

So in this case you might target a separate UT project for each of them (although, again, that might be tricky to do with EF), and a single IT project that actually goes through the whole workflow. 因此,在这种情况下,您可能会为每个项目定位一个单独的UT项目(虽然,这可能与EF相比很棘手),以及实际完成整个工作流程的单个IT项目。

In my experience you should unit test (or integration for the data layer) each layer separately and thoroughly. 根据我的经验,您应该分别彻底地对每一层进行单元测试(或数据层的集成)。 That's first, but once you have that in place it's a good idea to have a few end to end test for your actual product's features. 这是第一次,但是一旦你有了这个,那么对你的实际产品功能进行一些端到端的测试是个好主意。

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

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