简体   繁体   中英

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?

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.

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.

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

Perhaps you meant to return a CommandAction from Translate ?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM