Testing an ASP.NET MVC using XUnit-Part 2
In this section we talk about unit test and development of a program in which we use unit testing:
What is Unit Testing?
Let’s try to find the exact definition of the Unit.
UNIT
In this IT world a unit refers to simply a smallest piece of code which takes an input ,does certain operation, and gives an output , And testing this small piece of code is called Unit Testing.
Write your first tests
National code validation method:
We consider the tenth digit of the national number (from the left) as TempA. Consider a value of TempB and set it to =
(First digit * 10) + (second digit * 9) + (third digit * 8) + (fourth digit * 7) + (fifth digit * 6) + (sixth digit * 5) + (seventh digit * 4) + ( Eighth digit * 3) + (Ninth digit * 2)
Set the value of TempC to = TempB — (TempB / 11) * 11.
If the value of TempC is zero and the value of TempA is equal to TempC, the national code is correct. If the value of TempC is 1 and the value of TempA is 1, the national code is correct. If the value of TempC is greater than 1 and the value of TempA is equal to 11 — TempC, the national code is correct.
The following is a method written in C # .NET.
public static class Helpers
{
/// <summary>
/// Check Validation of National code
/// </summary>
/// <param name="nationalCode">National code </param>
/// <returns>
/// if (nationalcode is valid) return true
else return false /// </returns>
/// <exception cref="System.Exception"></exception>
public static Boolean IsValidNationalCode(this String nationalCode)
{
//if natinal code is empty
if (String.IsNullOrEmpty(nationalCode))
throw new Exception("insert valid national code");
//length of national code is less than 10
if (nationalCode.Length != 10)
throw new Exception("lenth of national code equal 10");
//all character must number in national code
var regex = new Regex(@"\d{10}");
if (!regex.IsMatch(nationalCode))
throw new Exception("enter valide national code ");
//if all numbers is same
var allDigitEqual = new[]{"0000000000","1111111111","2222222222","3333333333","4444444444","5555555555","6666666666","7777777777","8888888888","9999999999"};
if (allDigitEqual.Contains(nationalCode)) return false;
var chArray = nationalCode.ToCharArray();
var num0 = Convert.ToInt32(chArray[0].ToString())*10;
var num2 = Convert.ToInt32(chArray[1].ToString())*9;
var num3 = Convert.ToInt32(chArray[2].ToString())*8;
var num4 = Convert.ToInt32(chArray[3].ToString())*7;
var num5 = Convert.ToInt32(chArray[4].ToString())*6;
var num6 = Convert.ToInt32(chArray[5].ToString())*5;
var num7 = Convert.ToInt32(chArray[6].ToString())*4;
var num8 = Convert.ToInt32(chArray[7].ToString())*3;
var num9 = Convert.ToInt32(chArray[8].ToString())*2;
var a = Convert.ToInt32(chArray[9].ToString());
var b = (((((((num0 + num2) + num3) + num4) + num5) + num6) + num7) + num8) + num9;
var c = b%11;
return (((c < 2) && (a == c)) || ((c >= 2) && ((11 - c) == a)));
}
}
Now , my test function is ready and we can write unit test for this function.
after write this test code , build project and run test methods like below :
We can write unit test for throw exception like below :
If we need a list of data or need database for test function we can use Mock for create fake dataset to use in test.