我需要断言三个值是相同的.
例如,我需要这样的东西
Assert.AreEqual(person1.Name, person2.Name, person3.Name);
Assert.That(person1.Name, Is.EqualTo(person2.Name), Is.EqualTo(person3.Name));
这两个方法仅允许比较两个值(前两个参数),第三个参数是输出消息.
我了解CollectionAssert,但我不想仅为这种情况创建IEnumerables.
是否可以在不使用集合的情况下比较NUnit中的两个以上参数?接受params []或其他方法的某种方法.
解决方法:
您可以执行2个单独的断言:
Assert.AreEqual(person1.Name, person2.Name);
Assert.AreEqual(person1.Name, person3.Name);
或者,您可以创建一个辅助函数:
public static class AssertEx
{
public static void AllAreEqual<T>(params T[] items)
{
for (int i = 1; i < items.Length; i++)
{
Assert.AreEqual(items[0], items[i]);
}
}
}
您可以这样使用:
[Test]
public void TestShouldPass()
{
var person1 = new Person { Name = "John" };
var person2 = new Person { Name = "John" };
var person3 = new Person { Name = "John" };
AssertEx.AllAreEqual(person1.Name, person2.Name, person3.Name);
}
[Test]
public void TestShouldFail()
{
var person1 = new Person { Name = "John" };
var person2 = new Person { Name = "Bob" };
var person3 = new Person { Name = "John" };
AssertEx.AllAreEqual(person1.Name, person2.Name, person3.Name);
}