Difference between revisions of "Talk:DATA"
(New page: Using PrivateObject to test private methods, fields or properties) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | 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<br> | ||
+ | |||
+ | <u>Note</u>: To use PrivateObject, you have to add Microsoft.VisualStudio.TestTools.UnitTesting dll to DomainTester. <br> <br>PrivateObject Class: | ||
+ | |||
+ | * Invoke method: Used to access the members of the private object. | ||
+ | |||
+ | eg: <br> YourClass class = new YourClass();<br> var privateObject = new PrivateObject(class);<br> 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). | ||
+ | |||
+ | <br> | ||
+ | |||
+ | * GetProperty method: Used to get the value of the property of the wrapped object | ||
+ | |||
+ | eg: <br> YourClass class = new YourClass();<br> var privateObject = new PrivateObject(class);<br> privateObject.GetProperty("YourPrivatePropertyName", null); | ||
+ | |||
+ | The code above will call the your private property. This is the equivalent of calling resel.YourPrivatePropertyName <br> <u>Note</u>: The second argument is a property index <br> private int Column[int index] <br> In this case, you will get private property by using privateObject.GetProperty("YourPrivatePropertyName", your index); | ||
+ | |||
+ | <br> | ||
+ | |||
+ | * SetProperty method: Used to set the value of the property of the wrapped object | ||
+ | |||
+ | eg: <br> YourClass class = new YourClass();<br> var privateObject = new PrivateObject(class);<br> 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 <br> <u>Note</u>: 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 |
Latest revision as of 10:32, 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 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