-
Notifications
You must be signed in to change notification settings - Fork 19
Partial Comparisons
Derek Greer edited this page Sep 13, 2017
·
5 revisions
The ability to compare two instances of the same type without encumbering production code with test-specific equality methods is often helpful when writing tests, but in some cases only a subset of the properties for a given object need to be verified. ExpectedObjects facilitates this scenario by allowing comparisons against an anonymous object which defines only a subset of the expected object graph through the ShouldMatch extension method.
The following example shows how a subset of the expected values for a Customer object may be verified:
using ExpectedObjects;
using Xunit;
namespace MyProject.Specs
{
public class CustomerSpecs
{
[Fact]
public void ComparingEqualCustomers_ShouldBeEqual()
{
// establish context
var expectedCustomer = new
{
FirstName = "Silence",
Address = new
{
City = "Boston",
State = "MA",
PostalCode = "02114"
}
}.ToExpectedObject();
var actualCustomer = new Customer
{
FirstName = "Silence",
LastName = "Dogood",
Address = new Address
{
AddressLineOne = "The New-England Courant",
AddressLineTwo = "3 Queen Street",
City = "Boston",
State = "MA",
PostalCode = "02114"
}
};
// observation
expectedCustomer.ShouldMatch(actualCustomer);
}
}
}