当我有一个 Dictionary<string, int> actual
,然后创建一个完全相同值的新的 Dictionary<string, int> expected
。
调用
Assert.That(actual, Is.EqualTo(expected));
会使测试通过。当使用
Assert.That(actual, Is.EquivalentTo(expected));
时,测试不会通过。
EqualTo()
和 EquivalentTo()
之间的区别是什么?
编辑:
当测试不通过时的异常消息如下:
Zoozle.Tests.Unit.PredictionTests.ReturnsDriversSelectedMoreThanOnceAndTheirPositions:
Expected: equivalent to < [Michael Schumacher, System.Collections.Generic.List`1[System.Int32]] >
But was: < [Michael Schumacher, System.Collections.Generic.List`1[System.Int32]] >
我的代码看起来像这样:
[Test]
public void ReturnsDriversSelectedMoreThanOnceAndTheirPositions()
{
//arrange
Prediction prediction = new Prediction();
Dictionary<string, List<int>> expected = new Dictionary<string, List<int>>()
{
{ "Michael Schumacher", new List<int> { 1, 2 } }
};
//act
var actual = prediction.CheckForDriversSelectedMoreThanOnce();
//assert
//Assert.That(actual, Is.EqualTo(expected));
Assert.That(actual, Is.EquivalentTo(expected));
}
public Dictionary<string, List<int>> CheckForDriversSelectedMoreThanOnce()
{
Dictionary<string, List<int>> expected = new Dictionary<string, List<int>>();
expected.Add("Michael Schumacher", new List<int> { 1, 2 });
return expected;
}
Assert.That()
抛出异常并提供详细的错误信息。请查看该信息。 - abatishchev