1

The following links suggest that this should work.

  1. Compare equality between two objects in NUnit

  2. NNunt Documentation

    [TestFixture()]
    public class ResponseMessageUnitTests
    {
    
        private Mock<IConfiguration> _IConfigurationMoq;
        private Mock<ICardReader> _ICardReaderMoq;
        private ResponseMessage responseMessage; 
    
        public ResponseMessageUnitTests()
        {
            //Act
            this._IConfigurationMoq = new Mock<IConfiguration>();
            _IConfigurationMoq.Setup(x => x.REPEATDELAYTIME).Returns(0);
            _IConfigurationMoq.Setup(x => x.COLOUR_WHOLE_RESPONSE_AREA).Returns(true);
            this._ICardReaderMoq = new Mock<ICardReader>();
            this.responseMessage = new ResponseMessage(_IConfigurationMoq.Object, _ICardReaderMoq.Object);
        } 
    
        [Test(Description =
        "Calling ResponseMessage DisplayAdditionalInfo_Empty Method " +
        "with a Background Colour that populates the fields " +
        "AdditionalInfoLine[1-4] and " +
        "AdditionalInfoNo[1-4]")]
        [TestCase(BACKGROUNDCOLOURS.GOOD)]
        public void ResponseMessage_DisplayAdditionalInfo_Empty(string backgroundColour)
    
        {
            //Arrange
            _IConfigurationMoq.Setup(x => x.COLOUR_WHOLE_RESPONSE_AREA).Returns(true);
            _IConfigurationMoq.Setup(x => x.REPEATDELAYTIME).Returns(0);
    
            DisplayLine expected = new DisplayLine(_IConfigurationMoq.Object, null, null, backgroundColour);
    
            //Act
            responseMessage.DisplayAdditionalInfo_Empty(backgroundColour);
    
            //Assert
            Assert.AreEqual(expected,responseMessage.AdditionalInfoLine1);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoLine2);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoLine3);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoLine4);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoNo1);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoNo2);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoNo3);
            //Assert.AreEqual(expected, responseMessage.AdditionalInfoNo4);
        }
    

If I place a breakpoint on the line with the Assert.AreEqual, I get the following:

The objects being compared

The tests fails to my surprise with the following error message:

Message:   Expected: <EPIC.APIS.REST.Models.Android.DisplayLine>
           But was:  <EPIC.APIS.REST.Models.Android.DisplayLine>

I don't understand why... The 2 objects are of the same type and contain the same values?

 <package id="NUnit" version="3.12.0" targetFramework="net462" />
Charlie
  • 12,928
  • 1
  • 27
  • 31
Craig Gers
  • 544
  • 3
  • 9
  • Does your `DisplayLine` class override `Equals`? – Klaus Gütter Dec 18 '20 at 17:57
  • No, it does not have an Equals override. – Craig Gers Dec 18 '20 at 18:05
  • Your link to https://docs.nunit.org/2.2.2/comparisonAsserts.html is legacy documentation... It even says so at the top of the page! I edited your question so that the link goes to the current documentation. Please try to avoid confusing yourself and others with obsolete docs - NUnit 2.2.2 is from 2004! – Charlie Dec 18 '20 at 23:45

1 Answers1

1

well as the threads you linked to suggest, Assert.AreEqual compares as a reference equal unless you override it.

So even though everything looks the same, they're probably different object / references.

You could have a look at the PropertyValuesAreEquals from this answer: https://stackoverflow.com/a/318238/919324 and either compare your objects like that, or override the comparison of the objects

Ron Sijm
  • 8,490
  • 2
  • 31
  • 48
  • To get an intelligent error message, you should also override ToString() on the object. If you don't do that, only the class name is shown for user-defined Types. – Charlie Dec 18 '20 at 23:46
  • Great suggestion. What I did was create an Equals method for my DisplayLine class. I could then call this Equals method in my test. Assert.IsTrue(expected.Equals(responseMessage.AdditionalInfoLine1)); – Craig Gers Dec 21 '20 at 10:39