我有一个简单的单元测试方法,比较包含 TimeSpan 值的 2 个列表,我们还使用 Moq 来初始化测试:
private IRepository _RepositoryTimeBand;
[TestInitialize]
public void TestInit()
{
var TimeBandSet = new List<TimeBand>
{
new TimeBand() {StartTime = new TimeSpan(7, 30, 0), EndTime = new TimeSpan(16, 0, 0)},
new TimeBand() {StartTime = new TimeSpan(19, 0, 0), EndTime = new TimeSpan(21, 0, 0)}
};
var RepositoryMoq = new Mock<IRepository>();
RepositoryMoq.Setup(
mr =>
mr.GetTimeBand().Returns(TimeBandSet);
_RepositoryTimeBand = RepositoryMoq.Object;
[TestMethod]
public void GetTimeBand_ExpectListOfTimeBandReturned()
{
var expected = new List<TimeBand>
{
new TimeBand {StartTime = new TimeSpan(7, 30, 0), EndTime = new TimeSpan(16, 0, 0)},
new TimeBand {StartTime = new TimeSpan(19, 0, 0), EndTime = new TimeSpan(21, 0, 0)}
};
var timeBandList = _RepositoryTimeBand.GetTimeBand();
Assert.IsNotNull(timeBandList);
Assert.IsTrue(timeBandList.SequenceEqual(expected));
}
这就是 TimeBand 类的样子:
public class TimeBand
{
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
}
Assert.IsTrue(timeBandList.SequenceEqual(expected));
在上面的测试方法中,即使 2 个列表相同,也总是返回 false SequenceEqual
,比较 2 个列表的正确方法是什么?如果没有,还有其他方法可以比较这些列表吗?
谢谢