我将在一个提供数据并使用大量数组的 API 上担任测试员,因此我决定使用 NUnit、LINQ 和 C# 进行一些练习。
为了帮助我“可视化” actual
GruopJoin 的结果,作为具有嵌套数组的数组,我正在制作一个等效expected
对象,然后使用Assert
s.
理想情况下我会使用
Assert.That( actual, Is.EqualTo( expected ) );
但这失败并显示以下消息:
NUnitExamples.TestFixture1.LinqOuterJoinMultirelation_Anon:预期和实际都是索引 [0] 处的值不同预期:但是是:
我想这告诉我对象引用是不同的。我想知道的是,它们是等价的对象,即属性值都相等吗?
遍历数组元素属性,包括嵌套数组及其属性,告诉我对象是等效的,但如果非常冗长。有没有更简单的方法来做同样的事情?
这是我的代码(顺便说一句,欢迎对我的代码提出任何批评,因为我正在从新学习这一切):
[Test]
public void LinqOuterJoinMultirelation_Anon()
{
Course[] curriculum =
{
new Course() { CourseId = "C1", Title = "Database" },
new Course() { CourseId = "C2", Title = "HCI" },
new Course() { CourseId = "C3", Title = "Op Systems" },
new Course() { CourseId = "C4", Title = "Programming" }
};
ExamMark[] results =
{
new ExamMark() { StudentId = "S1", CourseId = "C1", Mark = 85 },
new ExamMark() { StudentId = "S1", CourseId = "C2", Mark = 49 },
new ExamMark() { StudentId = "S1", CourseId = "C3", Mark = 85 },
new ExamMark() { StudentId = "S2", CourseId = "C1", Mark = 49 },
new ExamMark() { StudentId = "S3", CourseId = "C3", Mark = 66 },
new ExamMark() { StudentId = "S4", CourseId = "C1", Mark = 93 }
};
var actual = curriculum.GroupJoin
(
results,
c => c.CourseId,
r => r.CourseId,
(c, r) => new Performance() { CourseId = c.CourseId, Results = r.ToArray<ExamMark>() }
).ToArray<Performance>();
var expected = new[]
{
new Performance
{
CourseId = "C1",
Results = new[]
{
new ExamMark() { StudentId = "S1", CourseId = "C1", Mark = 85 },
new ExamMark() { StudentId = "S2", CourseId = "C1", Mark = 49 },
new ExamMark() { StudentId = "S4", CourseId = "C1", Mark = 93 }
}
},
new Performance
{
CourseId = "C2",
Results = new[]
{
new ExamMark() { StudentId = "S1", CourseId = "C2", Mark = 49 }
}
},
new Performance
{
CourseId = "C3",
Results = new[]
{
new ExamMark() { StudentId = "S1", CourseId = "C3", Mark = 85 },
new ExamMark() { StudentId = "S3", CourseId = "C3", Mark = 66 }
}
},
new Performance { CourseId = "C4" }
};
//Assert.That( actual, Is.EqualTo( expected ) );
for (int i = 0; i < actual.Count(); i++)
{
Assert.That( actual[i].CourseId, Is.EqualTo( expected[i].CourseId ) );
for (int j = 0; j < actual[i].Results.Count(); j++)
{
Assert.That( actual[i].Results[j].StudentId, Is.EqualTo( expected[i].Results[j].StudentId ) );
Assert.That( actual[i].Results[j].CourseId, Is.EqualTo( expected[i].Results[j].CourseId ) );
Assert.That( actual[i].Results[j].Mark, Is.EqualTo( expected[i].Results[j].Mark ) );
}
}
class Course
{
public string CourseId { get; set; }
public string Title { get; set; }
}
class ExamMark
{
public string StudentId { get; set; }
public string CourseId { get; set; }
public int Mark { get; set; }
}
class Performance
{
public string CourseId { get; set; }
public ExamMark[] Results { get; set; }
}
解决
我更改了要实现的类,IEquatable<T>
包括覆盖,Equals
并发现我的expected
对象不等同于actual
由于空数组引用而不是零元素数组,即需要从
new Performance { CourseId = "C4" }
至
new Performance { CourseId = "C4" , Results = new ExamMark[0] }
现在Assert.That( actual, Is.EqualTo( expected ) );
按预期工作:)