Ross
Ross

Reputation: 325

Assert.AreEqual failed during unit test

I am running the following test for a simple console game project I am working on which is failing with Assert.AreEqual failed.

[TestMethod]
    public void TestMethod1()
    {
        //Arrange
        CommandInterpreter interpreter = new CommandInterpreter(new List<IDungeonObject>());
        CommandAction commandAction = new CommandAction(CommandEnum.GoNorth, null);
        const string north = "Go North";
        var expected = commandAction; 

        //Act
        var ogbjecUndertest = interpreter.Translate(north);

        //Assert
        Assert.AreEqual(expected, ogbjecUndertest);

    }

Basically the test passes a string (north in this case) to the Translate(north) method which then calls the following code and returns an Enum depending on the string.

public CommandAction Translate(string issuedCommand) // <-- Go North
    {
        if (issuedCommand.IndexOf(' ') == -1)
            throw new InvalidCommandException("No command entered");

        if (issuedCommand.Equals("Go South", StringComparison.OrdinalIgnoreCase))
            return new CommandAction(CommandEnum.GoSouth, null);

        if (issuedCommand.Equals("Go North", StringComparison.OrdinalIgnoreCase))
            return new CommandAction(CommandEnum.GoNorth, null);

        return null;

The method takes in the CommandAction class of

 public class CommandAction
{
   #region Properites

   /// <summary>
   /// Defines a valid commands
   /// </summary>
   public CommandEnum Command { get; private set; }

   /// <summary>
   /// Defines a dungeon object
   /// </summary>
   public IDungeonObject RelatedObject { get; private set; }

   #endregion

   #region Constructor

   /// <summary>
   /// User input action passed in by the dugeon controller 
   /// </summary>
   /// <param name="command"></param>
   /// <param name="dungeonObject"></param>
   public CommandAction(CommandEnum command, IDungeonObject dungeonObject)
   {
       Command = command;
       RelatedObject = dungeonObject;
   }

   #endregion

}

When I debug the test the Assert expected shows my 2 CommandAction properties as; Command: GoNorth and RelatedObject null

And my object under test shows as; Command: GoNorth and RelatedObject: null

So my values look correct but I dont know why the test failing?

Upvotes: 1

Views: 1241

Answers (4)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38508

CommandAction is a reference type. Unless you override Equals method Assert.AreEqual will check for reference equality. Since you are creating the instance with new keyword inside the method, they will not be the same.

You can check the values of properties if you don't want to implement equality just for unit testing.

Assert.AreEqual(CommandEnum.GoNorth, ogbjecUndertest.Command);
Assert.IsNull(ogbjecUndertest.RelatedObject);

If you can use Fluent Assertions, ShouldBeEquivalentTo() method can do the work:

ogbjecUndertest.ShouldBeEquivalentTo(expected);

Upvotes: 1

ozz
ozz

Reputation: 5366

It is NOT the same object being returned by the method.

The check is doing a reference comparison and correctly telling you that it is not the same object.

Upvotes: 2

David Arno
David Arno

Reputation: 43264

You do not have a custom .Equals() for your CommandAction type, so AreEqual() is doing a reference comparison between the expected object and the new one created in Translate. They are different instances, so are not equal. The test is correctly failing.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 191058

Your expected CommandAction is not null but Translate is returning null.

Perhaps you meant to return a CommandAction from Translate?

Upvotes: 1

Related Questions