Код: Выделить всё
class ComponentDefinitionRoot: IEquatable
{
public string Id {get; set;}
public string Name {get; set;}
// many more properties...
public virtual bool Equals(ComponentDefinitionRoot? other)
{
if (other is null)
return false;
return Name == other.Name && Id == other.Id //... and so on;
}
}
Я реализовал его И это работает, но теперь я хочу проверить, правильно ли реализуется equals . Объекты, где только одно свойство отличается, чтобы убедиться, что равные работы: < /p>
Код: Выделить всё
[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void Equals_AreNotEqual_ReturnsFalse(ComponentDefinitionRoot unequalRoot)
{
var root1 = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
bool areEqual = root1.Equals(unequalRoot);
Assert.IsFalse(areEqual);
}
[TestMethod]
public void Equals_AreEqual_ReturnsTrue()
{
var root1 = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
var equalToRoot1 = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
bool areEqual = root1.Equals(equalToRoot1);
Assert.IsTrue(areEqual);
}
public static IEnumerable GetTestCases()
{
yield return new object[] { NameDifferent() };
yield return new object[] { IdDifferent() };
yield return new object[] { CreatedDateDifferent() };
yield return new object[] { SegmentNameDifferent() };
//and so on...
}
public static ComponentDefinitionRoot NameDifferent()
{
var result = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
result.Name = "not equal";
return result;
}
public static ComponentDefinitionRoot IdDifferent()
{
var result = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
result.Id = "000000000000000000000002";
return result;
}
public static ComponentDefinitionRoot CreatedDateDifferent()
{
var result = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
result.CreatedDate = GeneralMother.GetIndependenceDay();
return result;
}
public static ComponentDefinitionRoot SegmentNameDifferent()
{
var result = ComponentDefinitionEntityMother.MakeComponentDefinitionRootA();
result.Segments.First().Name = "unequal name";
return result;
}
Есть ли вы идеи?
Подробнее здесь: https://stackoverflow.com/questions/793 ... -class-has