YamlDotNet
YamlDotNet copied to clipboard
Testing serialization
Hi
I want to write some tests for serialization, my first naive approach is similar to the following code:
[Test]
public void Test_Deserialization()
{
MyClass instance = BuildTestInstance();
ISerializer serializer = BuildSerializer();
String output = serializer.Serialize(instance);
String expected = File.ReadAllText("expected.yaml");
output.Should().Be(expected);
}
But I found this to be extremely flaky: whitespaces are a little problematic, some some integer values in expected.yaml are written in hexadecimal and some other in decimal and the serializer outputs everything to decimal, etc.
Is there any recommended way to get the in memory representation of expected.yaml and the output of serialization, so I compare that instead of comparing the text?
I would suggest that you create an implementation of IEmitter that stores all the emitted events into a list and then serialize to an instance of that emitter. You will be able to make assertions against the event stream instead of having to compare text.