In my application, there is a method which accepts an Object
, then performs some operations on it to return a C# long
value. At runtime, the Object
received from Active Directory is an IADSLargeInteger
.
In writing a unit test for this code, I am unable to create such an object to pass into the method being tested.
How can I create such an object for my unit test? Are there other ways to verify the logic of my method?
Method to be tested:
public static long ConvertLargeIntegerToLong(object largeInteger)
{
var type = largeInteger.GetType();
var highPart = (int)type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null)!;
var lowPartInt = (int)type.InvokeMember("LowPart", BindingFlags.GetProperty | BindingFlags.Public, null, largeInteger, null)!;
uint lowPartUint;
unchecked
{
lowPartUint = (uint)lowPartInt;
}
return (long)highPart << 32 | (long)lowPartUint;
}
Sample Unit Test
public void ConvertLargeIntegerToLong_ComObjectLargeInt_Long()
{
var expectedValue = 94294967295;
var testValue = ??; // What to put here?
var result = ConvertLargeIntegerToLong(testValue);
Assert.AreEqual(expectedValue, result);
}
After asking the question, I continued hunting around and thought to add the activeds.dll
as a COM reference to my test project.
After I did that, I had direct access to the IADSLargeInteger
interface. And looking more closely at the Microsoft docs for the interface, saw an example creating such an object for VB.Net.
In the end, I did like this for my code (still maintaining the COM reference):
var testValue = new LargeInteger { HighPart = 1234, LowPart = 4567 };
LargeInteger
is also in that DLL and is the concrete class implementing the interface -- as @Hans Passant mentioned in his comment.