Tutorial :Questions on about TDD or unit testing in ASP.NET MVC



Question:

I've been searching on how to do Unit testing and find that it is quite easy, but, what I want to know is, In a asp.net mvc application, what should be REALLY important to test and which methods you guys use? I just can't find a clear answer on about WHAT TO REALLY TEST when programming unit tests. I just don't want to make unnecessary tests and lose development time doing overkill tests.


Solution:1

You should unit test as much as possible of your application.

For every line of code you write, you need to verify that it works. If you don't unit test it, you need to test it in some other fashion. Even starting up the site and clicking around is a sort of testing.

When you compare unit testing with other sorts of testing (including running the site and manually using it), unit tests tend to give the best return of investment because they are relatively easy to write and maintain, and can give you rapid feedback on whether you just introduced a regression bug or not.

I'm not saying that there's no overhead in writing unit tests - there is, but there's overhead in any sort of testing, and a big overhead in not testing at all (because regression bugs slip through quite easily).

It's still good practice to supplement unit tests with other types of tests, but a good unit test suite offers an excellent regression test suite.


Solution:2

Ron Jeffries says "Test everything that could possibly break."

Someone else - I think it was Kent Beck, but I can't find a reference - says, "Only test the code you want to work."

Either of these is a pretty good strategy.


Solution:3

I actually don't think anything needs to be tested in MVC. I think all your business logic, rules etc need testing but the Views and Controllers?

The only real reason I can see to test a Controller is for integration testing. If all your business logic is correct then that should be a simple test always returning true.

Controllers should really only get data from the view and pass data to it so....

As for views, what sort of testing can be done there other than to open the view and see what it does?

When I write my projects there is next to no code in the controller and I put all the grunt in my business engine which I have extensive tests for.


Solution:4

Unit testing is good for testing of services/models. But when you need in testing of application functionality the better choice will be functional tests (i.e. Selenium)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »