Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Deep) comparison of an object to a reference in unit tests (C#)
    text
    copied!<p>In a Unit Test (in Visual Studio 2008) I want to compare the content of a large object (a list of custom types, to be precise) with a stored reference of this object. The goal is to make sure, that any later refactorings of the code produces the same object content.</p> <p><em>Discarded Idea: A first thought was to serialize to XML, and then compare the hardcoded strings or a file content. This would allow for easy finding of any difference. However since my types are not XML serializable without a hack, I must find another solution. I could use binary serialization but this will not be readable anymore.</em></p> <p><strong>Is there a simple and elegant solution to this?</strong> <br><br><strong>EDIT:</strong> According to Marc Gravell's proposal I do now like this:</p> <pre><code>using (MemoryStream stream = new MemoryStream()) { //create actual graph using only comparable properties List&lt;NavigationResult&gt; comparableActual = (from item in sparsed select new NavigationResult { Direction = item.Direction, /*...*/ VersionIndication = item.VersionIndication }).ToList(); (new BinaryFormatter()).Serialize(stream, comparableActual); string base64encodedActual = System.Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length);//base64 encoded binary representation of this string base64encodedReference = @"AAEAAAD....";//this reference is the expected value Assert.AreEqual(base64encodedReference, base64encodedActual, "The comparable part of the sparsed set is not equal to the reference."); } </code></pre> <p>In essence I do select the comparable properties first, then encode the graph, then compare it to a similarly encoded reference. Encoding enables deep comparison in a simple way. The reason I use base64 encoding is, that I can easily store the reference it in a string variable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload