Monday, April 23, 2018

UNIT TEST in ASP.Net MVC Part II - How to handle App_GlobalResources


Traditional ASP.Net approaches to Localization and Internationaliazation is to use one special App_GlobalResources folder to hold resource files and Visual Studio will use GlobalResourceProxyGenerator to generate a strongly typed internal class to wrap all internal resources.

Note those global resources are not embeded inside the project dll file. They actually were stored inside one special assembly named App_GlobalResources.dll so that the ASP.Net views can reference them. This is fine but the problem is the special assembly was created by ASP.Net runtime, there is no such assembly during the compilation, thus when you tried to execute your unit testing against the resources, following error will happen:

"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.":"App_GlobalResources" System.IO.IOException {System.IO.FileNotFoundException}

The solution is to create one custom folder for your MVC project and put your resource files to that folder. For each resource file, update the file property as following:


The reason to change the Custom Tool to PublicResXFileCodeGenerator is because this tool will make the resource file available for your ASP.Net views. Alternately, you can also set the resource file to public inside the resource editor to do the same job.

Resource Files and ASP.NET MVC Projects

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

Wednesday, April 18, 2018

How to create a Restful API with authentication in Jwt

Asp.Net page usually do authentication and authorization with session. However, Restful means stateless, for doing so, we need to use Restful HTTP services to do authentication and authorization.