Why is my dynamic mock not working
I am trying to test a C# method that uses a dynamic property from a C++
CLR wrapper. The interface I am trying to mock is
property Object^ DynamicValueItem
{
[returnvalue: System::Runtime::CompilerServices::DynamicAttribute]
Object^ get () ;
}
The method I am trying to test is
public void GetBillInfo (IConfigurationItem item)
{
dynamic ValueItem = item.DynamicValueItem;
string Curr = ValueItem.Currency;
string Ser = ValueItem.BillSeries;
}
My test method is
[TestMethod()]
public void GetBillInfoTest()
{
BnrHelperMethods target = new BnrHelperMethods();
var ValueItem = new
{
Currency = "USD",
BillValue = 100,
};
var mockItem = new Mock<IConfigurationItem>();
mockItem.Setup(i => i.DynamicValueItem).Returns(ValueItem);
target.GetBillInfo(mockItem.Object);
}
I got the method for mocking the dynamic property from
http://blogs.clariusconsulting.net/kzu/how-to-mock-a-dynamic-object/
The example was for a standard C# dynamic property so I have had to adapt
my C++ CLR property to try and get the same effect. My problem is that
when I perform the test I get a RuntimeBinderException stating that the
object does not contain the definition of for Currency. If I look at the
Locals window it shows both Currency and BillValue
-ValueItem { Currency = USD, BillValue = 100 } dynamic{<>f__AnonymousType1}
-BillValue 0x00000064 int
-Currency "USD" string
When using the method normally it works. The only difference I see is that
Currency and BillValue are under a Dynamic View item in the Local window
-ValueItem {} dynamic {MEIConfiguration.ConfigurationValueItem}
-Dynamic View Expanding the Dynamic View will get the dynamic members for
the object
-BillValue 0x000003e8 System.Int32
-Currency "GBP" System.String
Have I defined the C++ property correctly? Am I creating the mock
correctly? Can anyone tell me what I am doing wrong?
Thanks Trevor
No comments:
Post a Comment