开发者

To achieve basic code coverage what tests should I run against an ASP.NET MVC Controller?

开发者 https://www.devze.com 2023-01-04 01:49 出处:网络
My controllers are utilizing StructureMap开发者_运维知识库 and AutoMapper. Presently, there is nothing exceptional with my routes. For the basic CRUD controller actions what tests should I be writing

My controllers are utilizing StructureMap开发者_运维知识库 and AutoMapper. Presently, there is nothing exceptional with my routes. For the basic CRUD controller actions what tests should I be writing to ensure good code coverage?


It is difficult to say what you have to test in your controllers without showing them. You say they are basic CRUD but then you talk about AutoMapper, so probably it's not as simple as that. Here's an example of unit test I wrote and the controller being tested.


I have been doing the same thing recently. From my research it seems that best practice is to create at least one test for each action (That way you cover CRUD by virtue of the fact that your actions are generally based on CRUD) and limit the test to the internal code. What that means is don't cross the method boundary but rather mock out everything your action needs and assert the desired results. Of course this means that you need to do the same thing for your services, repositories etc

But it means that you have a unit test that won't break if you change some code between the action and the DB unless you have a change to make. It is time consuming but so far I have found the effort well worth it as a failed unit test in unrelated code means I have a change to make I forgot about or I am too tightly coupled.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号