Talk:DATA

From Catglobe Wiki
Jump to: navigation, search

Test private methods, fields or properties by using the PrivateObject in the Microsoft Unit Testing. 


As you know, In Catglobe, we use Reflection to test some private fileds, properties or methods. After several minutes of googling, I found the better solution to solve this issue I think by using PrivateObject class in Microsoft.VisualStudio.TestTools.UnitTesting

Note: To use PrivateObject, you have to add Microsoft.VisualStudio.TestTools.UnitTesting dll to DomainTester.
 
PrivateObject Class: 

  •   Invoke method: Used to access the members of the private object.

      eg: 
         YourClass class = new YourClass();
         var privateObject = new PrivateObject(class);
         privateObject.Invoke("YourPrivateMethodName", new object[] { arg1, arg2 } );

         The code above will call the your private method and pass it two integer parameters. This is the equivalent of calling resel.YourPrivateMethodName(arg1, arg2).


  •   GetProperty method: Used to get the value of the property of the wrapped object

      eg: 
         YourClass class = new YourClass();
         var privateObject = new PrivateObject(class);
         privateObject.GetProperty("YourPrivatePropertyName", null);         

         The code above will call the your private property. This is the equivalent of calling resel.YourPrivatePropertyName 
         Note: The second argument is a property index 
                  private int Column[int index] 
                  In this case, you will get private property by using privateObject.GetProperty("YourPrivatePropertyName", your index);


  •   SetProperty method: Used to set the value of the property of the wrapped object

      eg: 
         YourClass class = new YourClass();
         var privateObject = new PrivateObject(class);
         privateObject.SetProperty("YourPrivatePropertyName", null, new value);

         The code above will call the your private property. This is the equivalent of calling resel.YourPrivatePropertyName = new value 
         Note: The second argument is a property index .

If you want to know in detail, you might visit msnd with link is : http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.privateobject(VS.80).aspx