Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:52:27+00:00 2026-06-08T04:52:27+00:00

Today I changed to use StringDictionary instead of Dictionary<string,string> in my code, but old

  • 0

Today I changed to use StringDictionary instead of Dictionary<string,string> in my code, but old unit test failed. So I write a small unit test to test this.

Here is my small test:

using Rhino.Mocks;
using NUnit.Framework;
//using CustomDictionary = System.Collections.Specialized.StringDictionary;
using CustomDictionary = System.Collections.Generic.Dictionary<string, string>;

namespace ConsoleApplication1
{
    public interface ITest
    {
        void DoSth(CustomDictionary dic);
    }
    public class OneTest : ITest
    {
        public void DoSth(CustomDictionary dic) {/*do nothing*/}
    }

    [TestFixture]
    public class TestClass
    {
        [Test]
        public void Test1()
        {
            var mockRepository = new MockRepository();
            var test = mockRepository.StrictMock<ITest>();

            using (mockRepository.Record())
            {
                Expect.Call(() => test.DoSth(new CustomDictionary { { "Test", "Test1" } }));
            }
            test.DoSth(new CustomDictionary { { "Test", "Test1" } });

            mockRepository.VerifyAll();
        }
    }
}

If I use Dictionary<string,string>, the test will pass, but when I use StringDictionary, the test failed.

What’s the problem here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:52:30+00:00Added an answer on June 8, 2026 at 4:52 am

    Problem goes from StringDictionary instances comparison. There is no overridden Equals method, thus instances compared not by content, but by references. If you will use same instance, test will pass:

        [Test]
        public void Test1()
        {
            var mockRepository = new MockRepository();
            var test = mockRepository.StrictMock<ITest>();
            var dictionary = new CustomDictionary { { "Test", "Test1" } };
    
            using (mockRepository.Record())            
                Expect.Call(() => test.DoSth(dictionary));
    
            test.DoSth(dictionary);
            mockRepository.VerifyAll();
        }
    

    You can override Equals on your CustomDictionary class to make your original test pass:

    public override bool Equals(object obj)
    {
        CustomDictionary other = obj as CustomDictionary;
        if (other == null)
            return false;
    
        if (Count != other.Count)
            return false;
    
        foreach (string key in Keys)
        {
            if (!other.ContainsKey(key))
                return false;
    
            if (this[key] != other[key])
                return false;
        }
    
        foreach (string key in other.Keys)
        {
            if (!ContainsKey(key))
                return false;
        }
    
        return true;
    }
    

    BTW I hope this is not your real test, because here you are testing mock, instead of testing your code.

    WHY YOUR CODE WORKS WITH DICTIONARY:

    As I understand RhinoMocks implementation, Rhino.Mocks.Impl.Validate class used for arguments validation. You can take a look on it’s ArgsEqual method implementation:

    public static bool ArgsEqual(object[] expectedArgs, object[] actualArgs)
    {
        return RecursiveCollectionEqual(expectedArgs, actualArgs);
    }
    

    I leave to you details of RecursiveCollectionEqual, but interesting part there is arguments comparison:

    if (current == null)
    {
        if (actual != null)
            return false;
    }
    else if (!SafeEquals(current, actual))
    {
        if (current is ICollection)
        {
            if (!RecursiveCollectionEqual((ICollection)current, (ICollection)actual))
                return false;
    
            continue;
        }
        return false;
    }
    

    As you can see, if argument is ICollection then Rhino goes deeper to compare expected and actual collections. Dictionary implements ICollection, but StringDictionary does not. Thus arguments of StringDictionary types compared only by reference.

    UPDATE: Didn’t notice, that you have an alias. Just inherit from type instead, and you will be able to override Equals:

    public class CustomDictionary : StringDictionary
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code was working earlier today, then randomly stopped, something must of changed, but
I wrote some code today and It was changed by another developer who said
While cleaning some code today written by someone else, I changed the access modifier
Today I changed the wordpress theme of my blog but the table, td style
Today I tried to use const indentifier, but I find the const variable can
I use my remote MySQL database during long time. But today I suddenly have
I had this app using 1.9.2 and rails 3.2 and today I changed the
My Zend application was running fine until today, when I changed something which caused
Sorry but I can't seem to get my special-kid helmet off today. I'm attempting
I've already looked myself but it seems my Google-fu is not strong today. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.