Difference between revisions of "Talk:DATA"

From Catglobe Wiki
Jump to: navigation, search
Line 1: Line 1:
Test private methods, fields or properties by using the PrivateObject in the Microsoft Unit Testing.
+
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. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Note: To use PrivateObject, you have to add Microsoft.VisualStudio.TestTools.UnitTesting dll to DomainTester. <br>&nbsp;<br>PrivateObject Class:&nbsp;
 +
 
 +
*&nbsp;&nbsp;&nbsp;Invoke method: Used to access the members of the private object.
 +
 
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex:&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YourClass class = new YourClass();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var privateObject = new PrivateObject(class);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;privateObject.Invoke("YourPrivateMethodName", new object[] { arg1, arg2 } );
 +
 
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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).
 +
 
 +
 
 +
 
 +
*&nbsp;&nbsp;&nbsp;GetProperty method: Used to get the value of the property of the wrapped object
 +
 
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex:&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YourClass class = new YourClass();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var privateObject = new PrivateObject(class);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;privateObject.GetProperty("YourPrivatePropertyName", null);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 +
 
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The code above will call the your private property. This is the equivalent of calling resel.YourPrivatePropertyName&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Note: The second argument is a property index&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private int Column[int index]&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this case, you will get private property by using privateObject.GetProperty("YourPrivatePropertyName", your index);
 +
 
 +
 
 +
 
 +
*&nbsp;&nbsp;&nbsp;SetProperty method: Used to set the value of the property of the wrapped object
 +
 
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex:&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YourClass class = new YourClass();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var privateObject = new PrivateObject(class);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;privateObject.SetProperty("YourPrivatePropertyName", null, new value);
 +
 
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The code above will call the your private property. This is the equivalent of calling resel.YourPrivatePropertyName = new value&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

Revision as of 10:22, 13 March 2009

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.
      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.

      ex: 
         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

      ex: 
         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

       ex: 
         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