chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm having a problem mocking a ResultPropertyCollection object. This object has two fields PropertyNames and PropertyValues. I can successfully mock this object, but it does not help very much - reason being ResultPropertyCollection inherits from DictionaryBase and the underlying hashtable has to be populated. I have read the limits of mscorlib assemblies, etc... but in my case I have to mock this hashtable.

This works, but doesn't seem like the easiest way .... I used a little reflection to add to the underlying hashtable.

Mock<ResultPropertyCollection> pc = MockManager.MockObject<ResultPropertyCollection>(Constructor.Mocked);

            ResultPropertyCollection rpc = pc.Object as ResultPropertyCollection;

            rpc.GetType().BaseType.GetInterface("IDictionary").InvokeMember("Add",
                                                        System.Reflection.BindingFlags.InvokeMethod,
                                                        null, rpc, new string[] { "test-key", "test-value" });


Is there an easier way?

Jayme
asked by Jayme Davis (600 points)

1 Answer

0 votes
Hi Jayme,

I'm not sure, I've completely understood you, but look at the following example:
[TestMethod]
public void MockIndexerTest()
{
    ResultPropertyCollection target = 
       RecorderManager.CreateMockedObject<ResultPropertyCollection>();
    using (RecordExpectations rec = new RecordExpectations())
    {
        string dum = target["test-key"];
        rec.Return("test-value").WhenArgumentsMatch();
    }
    string actual = target["test-key"];

    Assert.AreEqual("test-value", actual);
}


was this what you needed to do?
If not(which means that we have misread you), please add your test code.
It should explain better what you are trying to do.
answered by lior (13.2k points)
...