Why is my dynamic mock not working

1

I am trying to test a C# method that uses a dynamic property from a C++/CLI 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++/CLI 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++/CLI property correctly? Am I creating the mock correctly? Can anyone tell me what I am doing wrong?

c#
dynamic
c++-cli
moq
asked on Stack Overflow Sep 19, 2013 by Trevor • edited Sep 19, 2013 by IInspectable

1 Answer

1

For anyone who is interested here is the solution provided by a colleague.

[TestMethod()]
public void GetBillInfoTest()
{
    BnrHelperMethods target = new BnrHelperMethods();

    dynamic valueItem = new ExpandoObject();
    valueItem.Currency = "USD" ;
    valueItem.BillValue = 100;

    var mockItem = new Mock<IConfigurationItem>();

    mockItem.Setup(i => i.DynamicValueItem).Returns ((object)valueItem);

    target.GetBillInfo(mockItem.Object);
}
answered on Stack Overflow Oct 18, 2013 by Trevor

User contributions licensed under CC BY-SA 3.0