<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: LateApexEarlySpeed</title>
    <description>The latest articles on Forem by LateApexEarlySpeed (@lateapexearlyspeed).</description>
    <link>https://forem.com/lateapexearlyspeed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1342404%2F73ca8cb6-729d-41c0-819b-a19111bdb490.jpeg</url>
      <title>Forem: LateApexEarlySpeed</title>
      <link>https://forem.com/lateapexearlyspeed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lateapexearlyspeed"/>
    <language>en</language>
    <item>
      <title>Test JSON for .NET</title>
      <dc:creator>LateApexEarlySpeed</dc:creator>
      <pubDate>Sun, 24 Mar 2024 09:10:44 +0000</pubDate>
      <link>https://forem.com/lateapexearlyspeed/test-json-for-net-2ikc</link>
      <guid>https://forem.com/lateapexearlyspeed/test-json-for-net-2ikc</guid>
      <description>&lt;p&gt;Almost all the developers have requirement to write unit test. Sometimes you need to test whether specific JSON text is as expected, what will you decide to implement for it ?&lt;/p&gt;

&lt;p&gt;If you want to test whole JSON is same (means JSON-level equivalent) and meantime you have the data model which is just for coming JSON, you can test by normal way everyone knows: deserialize JSON to data model instance, then compare with expected another data model instance.&lt;/p&gt;

&lt;p&gt;But there should be other scenarios …&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you want to compare two JSONs are JSON-equivalent without creating additional data model&lt;/li&gt;
&lt;li&gt;Further, you want to assert some JSON nodes inside whole JSON body should meet some conditions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you know, even if 2 literal strings representing JSON are not text-level same, it is still possible that they are JSON-equivalent, just one simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "a": 1,
  "b": 2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "b": 2,
  "a": 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They are JSON-equivalent.&lt;/p&gt;

&lt;p&gt;Then how to deal with this sort of “equivalent” (Imagine possible recursive cases in future)?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is nuget package ‘&lt;strong&gt;LateApexEarlySpeed.Xunit.Assertion.Json&lt;/strong&gt;’, by using that, comparison can be achieved as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd1uo8vubh5cqvt9vlftx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd1uo8vubh5cqvt9vlftx.png" alt="Equivalent for simple JSON" width="355" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;this library also considered recursive and format cases:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi46i4xn7wsoaec50nurz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi46i4xn7wsoaec50nurz.png" alt="Equivalent for complex JSON" width="346" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When assertion failed, how to better know where the JSON node failed to assert and what is the cause ? Let’s try this assertion statement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdy4vpgvlc9ymj97w7by6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdy4vpgvlc9ymj97w7by6.png" alt="Different JSON comparison" width="342" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and then you will get following exception to tell that there are different number (&lt;strong&gt;2&lt;/strong&gt; and &lt;strong&gt;0&lt;/strong&gt;) in JSON location: &lt;strong&gt;/a/1&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb8gykaxawcw9ji4y8aj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb8gykaxawcw9ji4y8aj.png" alt="Assert exception" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The JSON location ‘&lt;strong&gt;/a/1&lt;/strong&gt;’ is with standard &lt;a href="https://datatracker.ietf.org/doc/html/rfc6901"&gt;JSON pointer&lt;/a&gt; format.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let’s say you don’t need to compare whole JSON body’s equivalent but have more concrete test requirement, for example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the JSON needs to have property ‘a’ with any value; have property ‘b’ with array with max length of 2 and each item should have property ‘prop’ with numeric value no large than 10; have property ‘c’ with a value be (JSON-) equivalent to {“c1”:1, “c2”:2}.&lt;/p&gt;

&lt;p&gt;No limit for other factors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;normally you may need to write test for this as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3dj1jl8fqrf5csv3iku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3dj1jl8fqrf5csv3iku.png" alt="Traditional way" width="692" height="608"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not only it costs more code, but also imagine if assertion failed inside ‘foreach’ loop, it is hard to get which array item error is from by exception.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With help of library ‘&lt;strong&gt;LateApexEarlySpeed.Xunit.Assertion.Json&lt;/strong&gt;’, test code can be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpy0xzqhvurl358dwpy0c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpy0xzqhvurl358dwpy0c.png" alt="New way" width="776" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code should be straightforward now and it is fluent manner.&lt;/p&gt;

&lt;p&gt;Notice the ‘Equivalent’ method mentioned in scenario 1 can also be used for any JSON node location.&lt;/p&gt;

&lt;p&gt;Sometimes you want to have custom validation logic unit in specific JSON node. Let’s say you want to&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;test value of property ‘c’ should have same numeric values for sub properties ‘c1’ and ‘c2’&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;then you can write as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F497capbsvk440v9gtwo9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F497capbsvk440v9gtwo9.png" alt="Support custom validation logic" width="800" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the ‘HasCustomValidation’ method accepts custom validation provider and custom error message provider and has several overloads to select.&lt;/p&gt;

&lt;p&gt;If property ‘c2’ is 2 in ‘actualJson’, then assertion will fail from your custom validation code and will get error message you provide:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe36vmmf7vsvllwysym3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe36vmmf7vsvllwysym3e.png" alt="Assert exception" width="693" height="43"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The available validation methods are in &lt;a href="https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema/wiki/Fluent-schema-builder#available-fluent-validation-methods"&gt;wiki&lt;/a&gt;, document is in &lt;a href="https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema"&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This article introduced one powerful and flexible JSON test way which should be able to cover most test requirement for JSON. Hope it helps, any feedback is welcomed and thanks for giving &lt;a href="https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema"&gt;github star&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;BTW, this JSON assertion extensions is based on core library which targets standard JSONSchema spec and the core library is also in same &lt;a href="https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema"&gt;repo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>json</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
