0

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.

abatishchev
101k88 gold badges303 silver badges443 bronze badges
asked Jun 29, 2011 at 9:14

2 Answers 2

6

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.

answered Jun 29, 2011 at 9:49
1
  • +1 Good answer. One of the best pieces of advice is to buy a good book on the subject and read it. I have saved a lot of time by doing this. Commented Jun 29, 2011 at 9:54
3

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

answered Jun 29, 2011 at 10:04
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.