Unit Testing in ASP.NET MVC

Pavithra M
Aug 26, 2017 · 2 min read

What is Unit Testing ?

Unit tests are definitely becoming a need-to-have skill for developers to confirm their code does what it should for production.

  • In real world every application comprises of many modules. For instance consider a simple E-Commerce application may comprises of Inventory Module, Customer management Module, Order module etc.
  • Every Module is made of using more than one classes.
  • Every class exposes many functionalities in the form of functions.

In Unit Testing we test these functions individually in an automated manner.

Get Easiest & Simplest idea about ASP.NET MVC

What are some general guidelines for Unit Testing?

Make sure you unit tests tests what needs to be unit tested…and nothing more.

If you have a simple POCO (Plain Old CLR Object) with a list of properties with no implementation or business rules behind it, I’m guessing you don’t need to test it.

Your code should be unit testable. If it’s not, you may need to rethink your design.

Sometimes, it’s a good idea to write your unit test first. That way, you get a clear understanding and perspective of how developers will interact with your code. If it’s not clear in your unit test, what makes you think that developers will understand it in production?

You really don’t need to mock everything.

Some users mock the heck out of everything because they can. For example, your business objects. No mocking is necessary. You can new one up just to see if code is structurally sound and passes a specific business requirement.

If a business routine or rule is really complex, write unit tests around it (or take a hammer to it

Confirm the nature of the routine by writing unit tests. If it’s really complicated and it’s a long method, “break” it into manageable pieces of code. Breaking this code smell into manageable chunks of code will accomplish two things: smaller, testable routines for unit testing and a better understanding of the code since it’s refactored.

Also Read

Asp.Net MVC Project Template

Asp.Net MVC New Features

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade