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

  • SEARCH
  • Home
  • 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 391239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:02:32+00:00 2026-05-12T16:02:32+00:00

Working with the fine mocking-framework MoQ, I came across a somewhat surprising facet (and

  • 0

Working with the fine mocking-framework MoQ, I came across a somewhat surprising facet (and I don’t like surprises).
I’m mocking out a class which should be added to a collection after a method-call, like so:

public class SomeClass{

}

public class Container {
    private List<SomeClass> classes = new List<SomeClass>();

    public IEnumerable<SomeClass> Classes {
        get {
            return classes;
        }
    }

    public void addSomeClass(SomeClass instance) {
        classes.Add(instance);
    }
}

[Test]
public void ContainerContainsAddedClassAfterAdd() {
    var mockSomeClass = new Mock<SomeClass>();  
    mockSomeClass.Setup(c => c.Equals(mockSomeClass.Object)).Return(true);

    var Container = new Container();
    Container.addSomeClass(mockSomeClass.Object);

    Assert(Container.Classes.Contains(mockSomeClass.Object));
}

This works well, the mock is added to the Container‘s collection and the setup of the Equals method on the mock makes sure the IEnumerable.Contains() return true.
However there’s always some complication. The class I’m really mocking out is not as simple as our SomeClass. It’s something like this:

public class SomeClassOverridingEquals{
    public virtual Equals(SomeClassOverridingEquals other) {
        return false;   
    }

    public override Equals(object obj) {
        var other = obj as SomeClassOverridingEquals;

        if (other != null) return Equals(other); // calls the override
        return false;
    }
}

[Test]
public void ContainerContainsAddedClassOverridingEqualsAfterAdd() {
    var mockSomeClass = new Mock<SomeClassOverridingEquals>();  
    mockSomeClass.Setup(c => c.Equals(mockSomeClass.Object)).Return(true);

    var Container = new Container();
    Container.addSomeClass(mockSomeClass.Object);

    Assert(Container.Classes.Contains(mockSomeClass.Object)); // fails
}

The class contains an override for the Equals method for its own specific type, and the Setup method for the mock does not seem to be able to mock out that specific method (only overriding the more general Equals(object)). Thus the test fails.

I have so far found no way of working around this quite common pattern, other than rewriting the class not to use the overriding equals.

I don’t like that.

Anyone have any ideas?

  • 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-05-12T16:02:32+00:00Added an answer on May 12, 2026 at 4:02 pm

    I don’t think the issue is with Moq, but rather with the Contains extension method. Even though you have overloaded Equals with a more specific overload, Enumerable.Contains ends up calling List<T>.Contains because the Classes property is really backed by a List<T>.

    List<T>.Contains is implemented by calling EqualityComparer<T>.Default.Equals, and I think that this defaults to calling the Equals method inherited from System.Object – that is: not the one your mock overrides, but the one that takes a System.Object as input.

    Perusing the implementation of EqualityComparer<T>.Default in Reflector, it seems as though it has a special case for types implementing IEquatable<T>, so if you add that interface to your class (it already has the appropriate method) it may behave differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Append() working fine but it is working only on the first <a class=add>Add</a> .
I've got autocomplete working fine, but I'm trying to figure out way to make
I am working with Moles and mocking a System.Data.Linq.Table. I got it constructing fine,
The paging is working fine here, the problem is I don't know how it
My code is working fine but I don't want it to fire when #stem
My app is working fine on 2.2 and 2.3 emulators. But when I tried
Everything is working fine until I call the saveFile method (shown below) to write
My procedure was working fine in a 2005 database, but my PC died and
It was working fine, now it's stopped. I select a symbol in my C
My website is working fine on IE, but when I open it on Firefox,

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.