0

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.

JohnGoldsmith
  • 2,638
  • 14
  • 26
  • Does this answer your question? [Comparing XmlDocument for equality (content wise)](https://stackoverflow.com/questions/2924350/comparing-xmldocument-for-equality-content-wise) – Panagiotis Kanavos Dec 02 '22 at 15:31
  • You should add a XmlWriter Settting and set Ident = true so you get the spaces. You can serialize to a Memory Stream and compare the stream with the original data. – jdweng Dec 02 '22 at 15:38
  • Thanks @PanagiotisKanavos - almost but I'm particularly looking for a diff with Verify. – JohnGoldsmith Dec 02 '22 at 15:38
  • Thanks @jdweng, the code is just to demo the serializing in and out, but really I'm after fitting this into the Verifiy test project – JohnGoldsmith Dec 02 '22 at 15:41
  • In that case you need to explain what you want. On one hand, Verify isn't *that* popular. On the other hand, either with XDocument or XmlDocument, `Verify(thatDoc)` should be able to compare the current and previous docs. Searching [the source code](https://github.com/VerifyTests/Verify/blob/c4b33b74e861f051046a541b54828a2592dc1dbc/src/Verify/Verifier/InnerVerifier_Xml.cs#L18) (because I don't know what Verify does) I see it even has a `VerifyXml` method that works on XDocument. – Panagiotis Kanavos Dec 02 '22 at 15:56
  • It's unclear what you want to verify too. `Person`? Or the XML generated by a specific XML serialization method? The question's code mixes the old-style XmlSerializer with the new style XDocument. Both XDocument and XmlDocument are *deserialized* types, not the product of serialization. They're used when you want to parse and handle an XML string as a document directly. – Panagiotis Kanavos Dec 02 '22 at 15:59
  • Looking further [I see there are 6 VerifyXml overloads](https://github.com/VerifyTests/Verify/blob/c4b33b74e861f051046a541b54828a2592dc1dbc/src/Verify.Xunit/Verifier_Xml.cs#L29) that work with either a Stream or String target. – Panagiotis Kanavos Dec 02 '22 at 16:05
  • And the repo's [XML tests](https://github.com/VerifyTests/Verify/blob/c4b33b74e861f051046a541b54828a2592dc1dbc/src/Verify.Tests/XmlTests.cs#L76) call `Verify` with both an XmlDocument and XDocument. What you want should be available out of the box – Panagiotis Kanavos Dec 02 '22 at 16:06
  • @Panagiotis you're right - I can see that the serialization demo code is not helpful / distracting. I was just trying to give a working demo of deserializing an xml document and re-serializing it back to xml. My working code does a lot more with XmlReader and XmlWriter and I'm wanting to use Verify to ensure that the output is the same as the input (and if it's not, to be able to quickly see where the two don't match). – JohnGoldsmith Dec 02 '22 at 18:57
  • Verify normally generates a received file as the output and, once the first run has been accepted, another verified file as the snapshot to test against future tests. What I'm wondering is, is there a way of doing this with Verify without effectively duplicating the input file? Ie, where the test is "check the output is the same as the input" can't the input serve as thing to test against. – JohnGoldsmith Dec 02 '22 at 18:57
  • I'm going to tag @Simon (Verify's creator) to see if he has some advice here? – JohnGoldsmith Dec 02 '22 at 18:58

0 Answers0