【发布时间】:2013-07-27 18:56:34
【问题描述】:
我有一个方法,我给出停车场(Entries,Exits)中汽车运动(Operation 类)的列表,每个运动都有一个相关的汽车列表(OperationVehicle 类)。这种方法应该会产生我所说的汽车日志。它列出了所有汽车,在什么时间进入什么时间退出以及每辆汽车的持续时间。 我对首先要测试的东西有点迷茫。这个问题的答案对我来说就像一个教程。
如何继续使用 tdd 对这个特定逻辑进行单元测试?
public class Operation
{
public Operation()
{
OperationVehicles = new List<OperationVehicle>();
}
public int OperationId { get; set; }
public DateTime Date { get; set; }
public virtual OperationType OperationType { get; set; }
public virtual List<OperationVehicle> OperationVehicles { get; set; }
}
public class OperationVehicle
{
public int OperationVehicleId { get; set; }
public OperationType OperationType { get; set; }
public virtual Operation Operation { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class CarLog
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Vehicle Vehicle { get; set; }
public int StayLength { get; set; }
}
public IEnumerable<CarLog> GenerateCarsLog(List<Operation> CarStayOperations)
{
// not implemented yet
return new List<CarLog>();
}
【问题讨论】:
-
与任何其他方法相同。您使用参数调用该方法,并检查它返回的内容是否正确。具体问题是什么?您最佳尝试的代码在哪里?测试方法的代码在哪里?
-
我并不拘泥于如何编写单元测试,而是要在这个特定的逻辑上测试什么案例。顺便说一句,我是单元测试的新手。我做了很多阅读。但我总是卡在现实世界的代码中。
-
先测试名义情况。然后,如果有特殊情况(空列表,或无效参数,或其他),这些也是如此。使用您喜欢的代码覆盖率工具来检测您是否遗漏了某些情况。
标签: c# unit-testing tdd logic