Use Verify snapshot package to test round tripped XML serialization

169 views Asked by At

I'd like to use Verify to test that a deserilized test XML document can be re-serialized and matches the original test document.

Given that the test document is the 'gold' version to test against is there a simple way to say verify against this instead of a generated one (.verified.txt)

For example, given a class like this:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

and a test method like this:

    [Fact]
    public async void Person_CanRoundTrip()
    {
        var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MyPerson.xml");
        XmlSerializer serializer = new XmlSerializer(typeof(Person));
        Person? p;

        // Deserialize xml
        await using (var fs = new FileStream(filePath, FileMode.Open))
        {
            p = (Person?)serializer.Deserialize(fs);
        }


        // Reserialize to test that the two xml documents match
        var generatedXDoc = new XDocument();
        await using (var xmlWriter = generatedXDoc.CreateWriter())
        {
            serializer.Serialize(xmlWriter, p);
        };

        //How can we tell Verify to use the original 'MyPerson.xml' to test against
        await Verify(generatedXDoc); 
    }

I guess one way is to copy the original file and append the .verified.txt suffix, but I'd also like to avoid being able to override this if a difference is found.

0

There are 0 answers