Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Underlying type is a hashtable which has to be populated

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 Jun 13, 2008 by Jayme Davis (120 points)

1 Answer

+1 vote
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 Jun 15, 2008 by lior (18,340 points)
...