Friday, April 20, 2018

UNIT TEST in ASP.Net MVC Part I - How to add Unit Test project and testing private or internal properties

Unit testing can greatly improve your confidence on your own code. Visual Studio has included unit testing framework since version 2015. You can add unit test project through following steps: right click on your solution, and choose Add, and choose New Projcet, Visual Studio will popup a window of model project, choose test under c# project on the left pane,  an unit test project will be added into your solution.

After you added the test project, you need to Add Reference for your project which has the object to be tested. Note if the attributes of the object you want to test are set as Internal, when you are trying to reference your object to test, system will promot following error:

'MyTest' is inaccessible due to its protection level

You need a special trick because your test project can't see the internal attribute of another project.

using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("MyTests")]
Add the last one to the project info file, e.g. Properties\AssemblyInfo.cs,for the project which has the object you want to test, while the "MyTests" is the name of your unit test project assembly. You can find your unit test project assembly name inside your unit test project info file, namely, AssemblyInfo.cs.

Note if you still experienced the error of "inaccessible due to protection level", you need to check the test project properties, in most cases, the project assembly name is different than what you thought. 

Now your unit test project can see the object to be tested.

Note if you still get
You can add

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using YourApplicationToTest;

namespace YourTest
{
    [TestClass]
    public class UnitTestl
    {
        [TestMethod]
        public void TestMethodl()
        {
            YourClassToTest ct= new YourClassToTest();
            Assert.AreEqual( ct.Name, "I need test!" );
        }
    }
}
Note if you want to test the private methods or attributes of your testing object, please use following:
Class target = new Class();
PrivateObject obj = new PrivateObject(target);
var retVal = obj.Invoke("PrivateMethod");
Assert.AreEqual((int)obj.GetField("PrivateAttribute"), 0);
Assert.AreEqual(retVal, expectedVal);
For test the private static attributes of your object, use following:
PrivateType myType = new PrivateType(typeof(MyStaticClass));

myType.SetStaticFieldOrProperty("isMyTypeAttribute", false);
UNIT TESTING in Asp.Net: Complete Tutorial
C# “internal” access modifier when doing unit testing
InternalsVisibleTo attribute isn't working
Unit testing private methods in C#
InternalsVisibleToAttribute Class
How to access a Static Class Private fields to unit test its methods using Microsoft Fakes in C#
Unit testing in C# of private-static method accepting other private-static method as a delegate parameter

No comments:

Post a Comment