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 7649827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:04:10+00:00 2026-05-31T11:04:10+00:00

I have a relatively simple abstract class. I’ve simplified it further for this question.

  • 0

I have a relatively simple abstract class. I’ve simplified it further for this question.

public abstract class BaseFoo
{
    public abstract string Identification { get; }
    //some other abstract methods

    public override bool Equals(object obj)
    {
        BaseFoo other = obj as BaseFoo;
        if(other == null)
        {
            return false;
        }
        return this.Identification.Equals(other.Identification);
    }
}

I’m trying to figure out how to write a unit test to ensure the object equals override works. I tried creating a mock, but when I cast the mock as an object and call Equals, it doesn’t call my code in the abstract class. It just immediately returns false. Same if I add it to a list of object and call .Remove or .Contains on the list; still just returns false without hitting the code in my abstract class.

I’m using mstest and rhino mocks.

For completeness, here is a test that I would expect to work but doesn’t:

[TestMethod]
public void BaseFoo_object_Equals_returns_true_for_Foos_with_same_Identification()
{
    var id = "testId";
    var foo1 = MockRepository.GenerateStub<BaseFoo>();
    var foo2 = MockRepository.GenerateStub<BaseFoo>();
    foo1.Stub(x => x.Identification).Return(id);
    foo2.Stub(x => x.Identification).Return(id);
    Assert.IsTrue(((object)foo1).Equals(foo2));
}
  • 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-31T11:04:12+00:00Added an answer on May 31, 2026 at 11:04 am

    Of course, I figured it out right after I post the question…

    I don’t know if this is the correct way to do this, but it seems to be working. I told the stub .CallOriginalMethod()

    [TestMethod]
    public void BaseFoo_object_Equals_returns_true_for_Foos_with_same_Identification()
    {
        var id = "testId";
        var foo1 = MockRepository.GenerateStub<BaseFoo>();
        var foo2 = MockRepository.GenerateStub<BaseFoo>();
        foo1.Stub(x => x.Identification).Return(id);
        foo2.Stub(x => x.Identification).Return(id);
        foo1.Stub(x => ((object)x).Equals(Arg<object>.Is.Anything)).CallOriginalMethod(OriginalCallOptions.NoExpectation);
        Assert.IsTrue(((object)foo1).Equals(foo2));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple question that I cannot seem to find the answer
I have a (hopefully) a relatively simple question. How do I tell Android which
So I have this relatively simple query to pull a random row based on
I'm seeing this issue . I have a relatively simple ASP.NET page with a
I have a relatively simple question that I cant figure out and I cant
This should have been a relatively simple one but I must be making a
I have a relatively simple question. Say I have a file but I only
I have this relatively simple regex for usernames // Enforce that username has to
I have a relatively simple class. I serialize it using JDK 1.7 on my
I have a general question about WPF performance. We have a relatively simple forms

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.