NUnit을 처음 접했고이 테스트가 실패한 이유에 대한 탐구를 찾고 있습니까?NUnit, CollectionAssert.AreEquivalent (..., ...), C# 질문
테스트를 실행할 때 다음 예외가 발생합니다.
은 NUnit.Framework.AssertionException는 : < < ....하여 ExampleClass>, < ....하여 ExampleClass>> 에 해당하지만했다 : 예상 < < ....하여 ExampleClass>, < ... .ExampleClass>> 당신은 당신의
ExampleClass
에Equals
및GetHashCode
를 구현해야
using NUnit.Framework;
using System.Collections.ObjectModel;
public class ExampleClass
{
public ExampleClass()
{
Price = 0m;
}
public string Description { get; set; }
public string SKU { get; set; }
public decimal Price { get; set; }
public int Qty { get; set; }
}
[TestFixture]
public class ExampleClassTests
{
[Test]
public void ExampleTest()
{
var collection1 = new Collection<ExampleClass>
{
new ExampleClass
{Qty = 1, SKU = "971114FT031M"},
new ExampleClass
{Qty = 1, SKU = "971114FT249LV"}
};
var collection2 = new Collection<ExampleClass>
{
new ExampleClass
{Qty = 1, SKU = "971114FT031M"},
new ExampleClass
{Qty = 1, SKU = "971114FT249LV"}
};
CollectionAssert.AreEquivalent(collection1, collection2);
}
}
Equals를 테스트 목적으로 만 구현하면 균등 오염이 발생할 수 있습니다. http://xunitpatterns.com/Test%20Logic%20in%20Production.html#Equality Pollution –
Equals 재정의가 수정되고 예제 테스트가 작동합니다. 그래서 GetHashCode() 메서드를 재정의하는 것이 좋은 습관입니까 아니면 다른 이유로 필요합니까? 감사! –
'GetHashCode' 구현체는 'Equals'와 일치해야합니다 : 두 객체가 같으면 같은 해시 코드를 가져야합니다. 두 객체는 서로 다른 해시 코드를 가질 수 있지만 서로 다른 객체가 동일한 해시 코드를 갖는 경우 'Equals'는 false를 반환해야합니다. –