vendredi 22 mai 2015

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?

Aucun commentaire:

Enregistrer un commentaire