Created
May 2, 2014 12:01
-
-
Save yreynhout/7c07e87e328ec0c7b08c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void EqualityMembersOverridden() | |
{ | |
new EqualityMembersOverriddenAssertion(Func<object> sutFactory). | |
EqualsCase(object other). | |
EqualsCase(object other). | |
NotEqualsCase(object other). | |
NotEqualsCase(object other). | |
NotEqualsCase(object other). | |
Verify(); | |
} |
If the type doesn't override Equals
and GetHashCode
use structural and/or semantic equality:
public class Projection
{
public string Property { get; set; }
}
public class StructuralEqualityBehaviorTests
{
[Test, AutoData]
public void ProjectionsAreDifferentInstances(
Projection sut,
Projection other)
{
Assert.AreNotSame(sut, other);
}
[Test, AutoData]
public void ProjectionsValuesAreEqual(
[Frozen]string expected,
Projection sut,
Projection other)
{
Assert.True(
expected == sut.Property &&
expected == other.Property);
}
[Test, AutoData]
public void ProjectionsAreNotEqual(
[Frozen]string expected,
Projection sut,
Projection other)
{
Assert.False(sut.Equals(other));
// Passes although it should fail - 'sut' and 'other' values are equal.
}
[Test, AutoData]
public void ProjectionsAreNotEqualEvenUsingNUnitSpecialSemantics(
[Frozen]string expected,
Projection sut,
Projection other)
{
Assert.AreNotEqual(sut, other);
// Passes although it should fail - 'sut' and 'other' values are equal.
}
[Test, AutoData]
public void ProjectionsAreEqualUsingStructuralEquality(
[Frozen]string expected,
Projection sut,
Projection other)
{
sut.AsSource().OfLikeness<Projection>().ShouldEqual(other);
}
}
The tests in the 1st
comment are using AutoFixture.Idioms.
The tests in the 2nd
comment are using SemanticComparison and AutoFixture.NUnit2.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the type overrides
Equals
andGetHashCode
correctly, compose an assertion of basic equality rules as below:Assuming that
ValueObject
is defined as: