Creating Unit Tests for ASP.NET MVC Controllers

In this blog post, we will learn to write unit tests for various MVC controller’s behaviors, such as view’s return types, redirecting to different actions etc. Specifically, we’ll cover writing Unit Tests for:

  1. ViewData of the Controller
  2. TempData of the Controller
  3. ViewBag of the Controller
  4. View of the Controller

Let’s get started!

Creating the MVC Project under Test

Let us go ahead and create a MVC project under Test. To keep things simpler, I have selected ”No Authentication” for the project. In this example we are going to write unit tests for the HomeController. The HomeController contains two action methods:

  1. Index
  2. Details

We are going to write tests for these actions. The HomeController contains code as shown here:

Read full article on the Infragistics Blog

Getting started with .NET unit testing using NUnit

It’s usually good practice to have automated unit tests while developing your code. Doing so helps you find bugs early in the development cycle and saves you time in the long run. These tests can be run by the developer multiple times to verify the behavior of a particular unit of code for different sets of input.

Unit tests essentially check the behavior of a particular unit of the code or the function, and are written by the developers who are implementing the functionalities.

image

We test the behavior of a function before it becomes part of the whole system and goes to production by writing a piece of code to test this behavior under different conditions. Usually a function gets tested in isolation with the other functions of the system under test (SUT).

Read full article on the Infragistics blog

How to Unit Test Private Methods in MS Test

Before we start to see, how a private method can be unit tested using the MS Test? Let us discuss whether it is a good idea to test a private method or not? Often I have seen there are two school of thoughts,

  1. Private methods should be tested.
  2. Private methods should not be tested.

To put these things in perspective, let us consider a system under test (SUT) Salary class as shown in the listing below.

image

A system under test Salary class has two functions:

  1. The CalculateSal method is a public method and it takes two parameters to calculate the salary
  2. The isValidwd method is a private method and it takes one parameter. This function returns true if the number of working days are more than 8, or else it returns false.
  3. The CalculateSal method first checks whether the number of working days is valid or not using the private method isValidWd.
  4. If the number of working days is valid, then salary is calculated by the multiplication of basic salary and the number of working days, or else it is fixed at $1000.

Read full article on the Infragistics blog

TestFixtureSetUp and TestFixtureTearDown in Unit Testing using NUnit: Part 4

We have been learning Unit Testing using NUnit together. Read first three parts of this series below,

How to do Unit Test using NUnit : Part 1

Test SetUp and TearDown in Unit Testing using NUnit : Part 2

Ignore Test in Unit Testing using NUnit: Part 3

In Part 2 , we discussed about TestSetUp and TearDown. If notice TestSetUP and TearDown is related to tests. They both get executed each time a test get executed. So if there are 5 tests in Test Fixture then TestSetUp and TearDown will get executed 5 times. Sometime this is good and needed for certain requirements. However doing heavy tasks like cleaning up whole database etc. each time for a test may not be a good idea. You may want to perform certain task for whole test fixture than for each and individual tests.

You can execute certain code before execution of all the tests in test fixture by using attributing that code with [TestFixtureSetUp]. This is used to create setup for all tests at one instant.

So you can create TestFixtureSetup or setup for whole test as following,

clip_image002

Basically we use TestFixtureSetUp to setup for all test. On the other hand you can clean up for all tests once using TestFixtureTearDown.

clip_image004

Using TestFixtureSetUp and TestFixtureTearDown are used to set up test and clean up test. I hope you find this post useful. Thanks for reading.

How to do Unit Test using NUnit : Part 1

Unit Testing is essential part of any production code. We can see rise of Test Driven Development (TDD) approach in many development projects. There are certain pros and cons of TDD but certainly we cannot ignore TDD way of software development. I read many blogs and tweets about TDD and got inspired to do some testing. I started learning to do unit testing of my codes. On searching I found NUnit an open source Unit Testing framework is the optimum way to start with Unit Testing. I downloaded NUnit from here and started my testing journey. After successful installation you will get NUnit GUI shown in below image,

image

What are we going to do Unit Test?

Before we move ahead in this blog post, let us discuss the code we are going to test. What to Unit Test and what not to Unit Test decision making is very simpler. If unit of code contains any logic then we must do unit testing of that code.

image

In this blog post we are going to test a basic function called DivideByZero. This function will return a false value if divisor is equal to zero. DivideByZero function is as following

image

How to do Unit Test ?

To write first Unit Test, we need to create a Test Project. To create a test project create a class library project. While writing Unit Test we should follow naming convention.

image

Since name of the project we are testing here is CalculatorLibraryToTest hence we will create test project with name CalculatorLibraryToTest.Test . Next we will create a class with the name CalcLibraryTest since function we are going to test is inside the class CalcLibrary.

Adhering to naming convention we can create test class as following ,

image

To tell NUnit explictity that this class is not a normal class rather a test class , we need to attribute the class with [TestFixture]

As of now we have written class that is going to hold the NUnit tests. Next we need to write Unit Test. We are going to test function DivideByZero and to create Test function we will follow naming convention.

  1. We are verifing that divisor parameter is zero or not . So this appened as second part of the test function.
  2. We are expecting if passed parameter is 0 then expected result would be false. So this is appened as third part of the function.

To tell NUnit explictly that this function is not a normal function rather a test function , we need to attribute the function with [Test]

 

image

Since we have test function is in place let us go ahead and write the code to test . In test we are creating object of the class CalcLibrary and then calling the function with required parameters.


using NUnit.Framework;

namespace CalculatorLibraryToTest.Test
{

[TestFixture]
public class CalcLibraryTest
{
[Test]
public void DivideByZero_IsDivisorZero_ReturnsFalse()
{
CalcLibrary  testobject = new CalcLibrary();
bool result = testobject.DivideByZero(56, 0);
Assert.IsFalse(result, "divisor passed is 0 ");

}
}
}

In above code we are using Assert. We will see in detail about Assert in further blog posts. Essentially we are asserting that returned result should be false.

To perform Unit Test go ahead and open NUnit project editor and open exsiting project. You need to provide test assemly name to test. Here we can see that unit test is passed.

image

In this way we can perform unit testing using NUnit. In further posts we will get into details of Uni Testing. I hope you find this post useful. Thanks for reading.