【发布时间】:2023-04-07 02:29:01
【问题描述】:
我正在为我正在处理的一个简单的控制台游戏项目运行以下测试,该项目因 Assert.AreEqual 失败而失败。
[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);
}
基本上,测试将一个字符串(在本例中为北)传递给 Translate(north) 方法,然后调用以下代码并根据字符串返回一个 Enum。
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;
该方法接受的CommandAction类
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
}
当我调试测试时,Assert 预期将我的 2 个 CommandAction 属性显示为;命令:GoNorth 和 RelatedObject null
我的测试对象显示为;命令:GoNorth 和 RelatedObject:null
所以我的值看起来是正确的,但我不知道为什么测试失败了?
【问题讨论】:
标签: c# unit-testing