开发者

Basic unit testing of ASP.NET MVC web app

开发者 https://www.devze.com 2023-03-16 16:15 出处:网络
What is the basic unit testing of a web app created using ASP.NET MVC and C#. I\'m using MySQL as my backend database. Do I need to create a unit test for the controller class and for the Model class?

What is the basic unit testing of a web app created using ASP.NET MVC and C#. I'm using MySQL as my backend database. Do I need to create a unit test for the controller class and for the Model class? I want to use NUnit framework and NMock framework. And as I read the articles for using the开发者_如何学Go NMock, I should use and Interface for my unit test but my codes doesn't have interface. Do I need to modify my web app codes and add some interface or there are another ways for this?

Please advise.

Many thanks.


If you have tight coupling in your code then you will find it hard to test each part of your application in isolation, and very difficult to mock dependencies.

So if your controller depends on SomeService then extract a new interface ISomeService and depend on that. This is where using an IoC container to inject your dependencies will also help you and promote loosely coupled code.

In a typical n-tier MVC application we would unit test our data access layer, service layer (mocking the data access layer dependency), MVC controllers (mocking the service layer dependency).

I don't tend to test my views/viewmodels because they are pretty dumb anyway.


When I first started MVC I read the Pro ASP.net MVC 2 book by Apress and I'd highly recommend it (Although the MVC 3 book comes out in a few weeks). It explains how to design your site so that it can be effectively unit tested. It also uses NUnit and Moq for testing. http://amzn.to/iIfij4

0

精彩评论

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