- 1. Xml
- 2. BinaryFormatterAssert
- 3. JSON
- 4. FieldAssert
- 5. FieldComparer<T>
Class exposing method for comparing xml strings.
XmlAssert.Equal(expectedXml, actualXml);
Compares two xml strings. If they are not equal an AssertException
is thrown.
The exception has a message that can give hints about what is wrong, sample:
Xml differ at line 4 index 21.
Expected: 4| <Value Attribute="1">2</Value>
But was: 4| <Value Attribute="1">Wrong</Value>
----------------------------------^
[Test]
public void EqualWithCustomElementComparer()
{
var expected = "<Foo>" +
" <Bar>1</Bar>" +
"</Foo>";
var actual = "<Foo>" +
" <Bar> 1.0 </Bar>" +
"</Foo>";
XmlAssert.Equal(expected, actual, new ElementDoubleValueComparer(), XmlAssertOptions.IgnoreDeclaration | XmlAssertOptions.IgnoreNamespaces);
}
private class ElementDoubleValueComparer : IEqualityComparer<XElement>
{
public bool Equals(XElement x, XElement y)
{
return double.Parse(x.Value, CultureInfo.InvariantCulture) == double.Parse(y.Value, CultureInfo.InvariantCulture);
}
int IEqualityComparer<XElement>.GetHashCode(XElement obj)
{
throw new System.NotImplementedException();
}
}
A collection of helpers for tests using XmlSerializer.
To test that an instance can be roundtripped use:
- Serializes actual to xml
- Deserializes xml
- Compares the instances using
FieldAsser.Equal
- Creates a
ContainerClass<T>
and roundtrips it. The reason we do this is to catch errors with ReadEndElement or reading outside the end of the element. - Returns the roundtripped instance in case you want to do additional asserts.
var actual = new Dummy { Value = 2 };
XmlSerializerAssert.RoundTrip(actual);
To assert that serialization produces the expected xml and that the instance can be roundtripped use:
var actual = new Dummy { Value = 2 };
var expectedXml = "<Dummy>\r\n" +
" <Value>2</Value>\r\n" +
"</Dummy>";
var roundtrip = XmlSerializerAssert.Equal(expectedXml, actual, XmlAssertOptions.IgnoreNamespaces | XmlAssertOptions.IgnoreDeclaration);
- Serializes actual to xml
- Calls XmlAssert.Equal(expectedXml, actual, options)
- Calls XmlSerializerAssert.Roundtrip(actual)
- Returns the roundtripped instance in case you want to do additional asserts.
To compare equality of the xml produced when serializing two instances use:
var expected = new Dummy { Value = 2 };
var actual = new Dummy { Value = 2 };
XmlSerializerAssert.Equal(expected, actual);
Checks if two instances produces the same xml. Useful for deep equals.
- Serializes expected and actual
- Calls XmlAssert.Equal(expectedXml, actualXml)
Serialize an object:
var dummy = new Dummy { Value = 2 };
string xml = XmlSerializerAssert.ToXml(dummy);
Deserialize xml to an object:
var foo = XmlSerializerAssert.FromXml(xml);
Same as XmlSerializerAssert but uses DataContractSerializer
Checks that T has [DataContract]
Checks that all properties for T has the [DataMember] attribute.
Use XmlAssertOptions
to specify how the the xml is compared.
Sample:
XmlAssert.Equal(expected, actual, XmlAssertOptions.IgnoreDeclaration | XmlAssertOptions.IgnoreNamespaces);
- Verbatim, the default and strictest mode.
- IgnoreDeclaration, ignore Xml declaration when comparing.
- IgnoreNamespaces, perhaps useful for less verbose tests.
- IgnoreElementOrder
- IgnoreAttributeOrder
- IgnoreOrder = IgnoreElementOrder | IgnoreAttributeOrder
Same as XmlSerializerAssert but uses BinaryFormatter
Same as XmlAssert but for JSON.
Same as XmlSerializerAssert but uses JsonSerializer
Checks that all fields are structurally equal recursively. Compares IEquatable<T>
types using object.Equals()
.
Compares collections by element.
var d1 = new Dummy { Value = 1 };
var d2 = new Dummy { Value = 1 };
FieldAssert.Equal(d1, d1);
If assert fails the output looks like this:
Found this difference between expected and actual:
expected[1].foo[2].bar.baz: 5
actual[1].foo[2].bar.baz: 2
NUnit's CollectionAssert takes an IComparer
CollectionAssert.AreEqual(actual, expected, FieldComparer.For<Foo>());