Skip to content

Instantly share code, notes, and snippets.

@bpruitt-goddard
Created November 28, 2018 20:29
Show Gist options
  • Save bpruitt-goddard/31302a2aec2bcdbbcb4967f0af9da901 to your computer and use it in GitHub Desktop.
Save bpruitt-goddard/31302a2aec2bcdbbcb4967f0af9da901 to your computer and use it in GitHub Desktop.
SemanticComparison Unit Test Example
public class SemanticTests
{
public class Foo
{
public int Bar { get; set; }
public string Baz { get; set; }
}
public class FooDto
{
public int Bar { get; set; }
public string Baz { get; set; }
}
[Fact]
public void TestSameClass()
{
var exp = new Foo { Bar = 1, Baz = "1" };
var act = new Foo { Bar = 1, Baz = "2" };
//Create proxy for better error messages
var expected = new Likeness<Foo, Foo>(exp)
.Without(x => x.Baz).CreateProxy();
Assert.Equal(expected, act);
var expectedAll = new Likeness<Foo, Foo>(exp).CreateProxy();
Assert.NotEqual(expectedAll, act);
}
[Fact]
public void TestInequalityMessage()
{
var exp = new Foo { Bar = 1, Baz = "1" };
var act = new Foo { Bar = 1, Baz = "2" };
var compAll = new Likeness<Foo, Foo>(exp);
compAll.ShouldEqual(act);
var expected = compAll.CreateProxy();
Assert.Equal(expected, act);
// Errors:
/*
* Message: SemanticComparison.LikenessException : The provided value Models.Tests.Unit.AddressTest+Foo
* did not match the expected value Models.Tests.Unit.AddressTest+Foo. The following members did not match:
* - Baz.
*/
}
[Fact]
public void TestDifferentClassesEquality()
{
var exp = new Foo { Bar = 1, Baz = "1" };
var act = new FooDto { Bar = 1, Baz = "1" };
var expected = new Likeness<Foo, FooDto>(exp);
// expected.ShouldEqual(act) will throw on error but returns void
// expected.CreateProxy() returns regular Foo, so will fail comparing to FooDto
//This passes, but doesn't show error when not equal
Assert.True(expected.Equals(act));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment