Monday, March 8, 2010

How to expose internal members of a class to Unit Test cases

Going by the convention of design patterns, it is always safe to have the members of a class as private or internal, In order to clearly abstract away the implementation details from the external world. The Classical example of this being Domain Model (Repository Pattern) where all the methods including constructors are made protected internal.

Example:

clip_image001

But there is a bottle neck in doing this. For maintainability purposes SUT (System under Test) (the internal method GetTime()in our case) and Unit test cases for testing the GetTime()) are placed in separate assemblies . Hence from the Unit Test cases, it is not possible to instantiate Class2 and test the GetTime() method .

This can be overcome by the InternalsVisibleTo attribute located in System.Runtime.CompilerServices namespace. The attribute takes the friend assembly’s name (the assembly to which the SUT has to be exposed) as an argument. The attribute when placed upon the namespace of the current assembly, is safely exposed to the target assembly (friend assembly).

Example:

clip_image002

clip_image003

This workaround is similar to the concept of Friend functions in C++, which is the only way by which a Class can access the private members of another Class(of course sparing pointers).

Saturday, March 6, 2010

Everything about AJAX

I was trying to read some articles relating to AJAX, and found this to be extremely useful.

http://ajaxtraining.blogspot.com/