domlao
domlao

Reputation: 16029

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.

Upvotes: 0

Views: 681

Answers (2)

Stephen Dryden
Stephen Dryden

Reputation: 571

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

Upvotes: 3

Ben Foster
Ben Foster

Reputation: 34800

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.

Upvotes: 6

Related Questions