简体   繁体   中英

Basic unit testing of ASP.NET MVC web app

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

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