Test if two IDictionary objects contain the same values I'm adding a function to my test library to assist in testing if two IDictionary objects contain the same keys/values.

I need the method to be generic and support a dictionary that has collections as values. So I thought I'd try to do it without using class types.

It appears to be working, but I would like to clarify that I didn't make any mistakes that would impact my tests later.

    /// &lt;summary&gt;
    /// Checks if two dictionaries contain the same values. Supports recursive
    /// dictionaries and collections as values.
    /// &lt;/summary&gt;
    /// &lt;param name="pExpect"&gt;Expected value&lt;/param&gt;
    /// &lt;param name="pActual"&gt;Actual value&lt;/param&gt;
    // ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
    public static void dictionary(IDictionary pExpect, IDictionary pActual)
    {
        Assert.IsNotNull(pExpect);
        Assert.IsNotNull(pActual);

        if (pExpect.Keys.Count != pActual.Keys.Count)
        {
            Assert.Fail("Expected {0} keys, but contains {1} keys.", pExpect.Keys.Count, pActual.Keys.Count);
        }

        object[] expectKeys = new object[pExpect.Keys.Count];
        object[] actualKeys = new object[pExpect.Keys.Count];

        pExpect.Keys.CopyTo(expectKeys, 0);
        pActual.Keys.CopyTo(actualKeys, 0);

        // check if the two key sets are the same
        CollectionAssert.AreEquivalent(expectKeys, actualKeys);

        for (int i = 0, c = expectKeys.Length; i &lt; c; i++)
        {
            object expect = pExpect[expectKeys[i]];
            object actual = pActual[actualKeys[i]];

            // both can be null
            if (expect == null &amp;&amp; actual == null)
            {
                continue;
            }

            // both must be assigned a value
            Assert.IsNotNull(expect);
            Assert.IsNotNull(actual);

            // must be same types
            Assert.AreEqual(expect.GetType(), actual.GetType());

            if (expect is IDictionary)
            {
                // support recursive dictionary checks
                dictionary((IDictionary)expect, (IDictionary)actual);
            }
            else if (expect is ICollection)
            {
                CollectionAssert.AreEquivalent((ICollection)expect, (ICollection)actual);
            }
            else
            {
                Assert.AreEqual(expect, actual);
            }
        }
    }


I am wondering if my last check Assert.AreEqual(expect, actual) will cover most remaining test cases.

EDIT: Fixed testing dictionaries that have keys in different orders.
Your code doesn't work, because items in a dictionary aren't ordered in any way.

Example code that fails your test:

dictionary(
    new Dictionary&lt;int, int&gt; { { 0, 0 }, { 1, 1 } },
    new Dictionary&lt;int, int&gt; { { 1, 1 }, { 0, 0 } });


Some more notes about your code:

public static void dictionary(IDictionary pExpect, IDictionary pActual)


dictionary is a bad name for this method, because it doesn't really explain what it's supposed to do. Something like DictionaryEqualityTest would be better.

Also, I would avoid using the non-generic IDictionary if possible, IDictionary&lt;TKey, TValue&gt; would be better (though it would complicate the recursion).