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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:41:01+00:00 2026-05-12T19:41:01+00:00

Today I ran into the following problem with NUnit. I have a class, that

  • 0

Today I ran into the following problem with NUnit.

I have a class, that derives from a generic class.
I started to do some serialization tests and tested for equality using the Is.EqualTo() function of NUnit.

I started to suspect something is wrong, when a test that should have failed passed instead. When I used obj1.Equals(obj2) instead it failed as it should.

To investigate I created the following tests:

namespace NUnit.Tests

{

using Framework;

    public class ThatNUnit
    {
        [Test]
        public void IsNotEqualTo_ClientsNotEqual_Passes()
        {
            var client1 = new DerrivedClient();
            var client2 = new DerrivedClient();

            client1.Name = "player1";
            client1.SomeGenericProperty = client1.Name;
            client2.Name = "player2";
            client2.SomeGenericProperty = client2.Name;

            Assert.That(client1.Equals(client2), Is.False);
            Assert.That(client1, Is.Not.EqualTo(client2));
        }

        [Test]
        public void IsNotEqualTo_ClientsAreEqual_AlsoPasses_SomethingWrongHere()
        {
            var client1 = new DerrivedClient();
            var client2 = new DerrivedClient();

            client1.Name = "player1";
            client1.SomeGenericProperty = client1.Name;
            client2.Name = client1.Name;
            client2.SomeGenericProperty = client1.Name;

            Assert.That(client1.Equals(client2), Is.True);
            Assert.That(client1, Is.Not.EqualTo(client2));
        }
    }

    public class DerrivedClient : Client<string>
    {
    }

    public class Client<T>
    {
        public string Name { get; set; }

        public T SomeGenericProperty { get; set; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof(Client<T>))
            {
                return false;
            }
            return Equals((Client<T>)obj);
        }

        public bool Equals(Client<T> other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return Equals(other.Name, Name) && Equals(other.SomeGenericProperty, SomeGenericProperty);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ SomeGenericProperty.GetHashCode();
            }
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}", Name, SomeGenericProperty);
        }
    }
}

The two (actually conflicting Asserts) in the second test show the problem:

Assert.That(client1.Equals(client2), Is.True);
Assert.That(client1, Is.Not.EqualTo(client2));

This test should fail one way or the other, but it doesn’t!

So I dug a little bit into NUnit’s source code, only to find, that after some if()s for some special conditions, the ObjectsAreEqual(object x, object y) method (which eventually gets called via Assert.That(x, Is.EqualTo(y)), comes to this line of code:

return x.Equals(y);

I find that very perplexing, since I must now think, that Is.EqualTo() just takes a longer route, but basically should be doing the same as x.Equals(y)

Here the full method for whoever is interested (inside the NUNit.Framework.Constraints namespace):

  public bool ObjectsEqual(object x, object y)
    {
        this.failurePoints = new ArrayList();

        if (x == null && y == null)
            return true;

        if (x == null || y == null)
            return false;

        Type xType = x.GetType();
        Type yType = y.GetType();

        if (xType.IsArray && yType.IsArray && !compareAsCollection)
            return ArraysEqual((Array)x, (Array)y);

        if (x is ICollection && y is ICollection)
            return CollectionsEqual((ICollection)x, (ICollection)y);

        if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
            return EnumerablesEqual((IEnumerable)x, (IEnumerable)y);

        if (externalComparer != null)
            return externalComparer.ObjectsEqual(x, y);

        if (x is string && y is string)
            return StringsEqual((string)x, (string)y);

        if (x is Stream && y is Stream)
            return StreamsEqual((Stream)x, (Stream)y);

        if (x is DirectoryInfo && y is DirectoryInfo)
            return DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y);

        if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            return Numerics.AreEqual(x, y, ref tolerance);

        if (tolerance != null && tolerance.Value is TimeSpan)
        {
            TimeSpan amount = (TimeSpan)tolerance.Value;

            if (x is DateTime && y is DateTime)
                return ((DateTime)x - (DateTime)y).Duration() <= amount;

            if (x is TimeSpan && y is TimeSpan)
                return ((TimeSpan)x - (TimeSpan)y).Duration() <= amount;
        }

        return x.Equals(y);
    }

So what is going on here and how can it be fixed?

I want to be able to trust my tests and thus necessarily NUnit again.

I also don’t want to start using Equals() instead of Is.EqualTo() (the former doesn’t give me such a nice output when the test fails).

Thanks in advance.

Update:

In the meantime I wrestled further with this problem and found a similar problem here and posted a possible workaround.

  • 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-12T19:41:02+00:00Added an answer on May 12, 2026 at 7:41 pm

    The problem is that the second assertion of the second test calls the Equals overload that accepts an object rather than a Client<T>, so this comparison returns false:

    // obj.GetType() returns Client.DerrivedClient
    
    if (obj.GetType() != typeof(Client<T>))
    {
        return false;
    }
    

    To fix this, you can change the comparison operation to this:

    if (obj.GetType() != this.GetType())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into an interesting issue today when I was working on an application
I ran into an issue today because I am developing an application in Visual
We have an application that is undergoing performance testing. Today, I decided to take
I installed SFML from scratch today so I could work on a game. I
I ran in to the unique situation today where I needed to bind the
I ran a leaks report on a program I am developing, with the following
Today I made a 64bit build of my project for the first time. Basically
A friend and I were a bit perplexed during a programming discussion today. As
I have been happily using VS 2010 for several weeks without any performance problems.
I just bought a new computer and I am trying to clone my heroku

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.