【发布时间】:2011-09-24 22:52:34
【问题描述】:
当我有一个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()抛出异常 + 给出错误的详细信息。请张贴。 -
我不认为我关注你。 Assert.That() 抛出异常?这是新的 nunit 语法——我认为它与旧模型没有什么不同。除此之外,你想让我把这个放在哪里?
-
他希望你在测试失败时将 NUnit 的输出添加到问题中。
-
如果断言失败,它会抛出相应的异常。不是吗?
-
好的,知道了 - 添加了代码和异常。
标签: .net unit-testing nunit nunit-2.5