简体   繁体   中英

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. In this case I just had 2 projects like SampleApplicationName and SampleApplicationName.Tests .

Now I am working in a team of 3 developers on a big project with really nice n-layer architecture. We have 5 projects in solution :

1.Data (POCO), 2.DataAccess (EF), 3.Common (helpers here), 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? 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?

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)
  • 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)

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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