<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C# Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/category/c-sharp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/category/c-sharp/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Fri, 29 Oct 2021 11:30:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>C# Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/category/c-sharp/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>Improve your tests with Assert Object Pattern</title>
		<link>https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/</link>
					<comments>https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 09 Nov 2020 20:42:34 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assert object]]></category>
		<category><![CDATA[assert object pattern]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit tests]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3888</guid>

					<description><![CDATA[<p>The Asserts Hell Let&#8217;s consider the following unit test: What&#8217;s wrong here? Given and When sections are great single-liners. We know straightaway what&#8217;s the input and the action executed. However, Then block is too complex. It&#8217;s hard to figure out, just passing quickly through this test, what is expected. This case is even not that&#8230;</p>
<p>The post <a href="https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/">Improve your tests with Assert Object Pattern</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Today I&#8217;d like to share with you a very interesting concept in software testing &#8211; Assert Object pattern. It makes the <em>Assert</em> part of a test much simpler and more readable. Let&#8217;s dive right into it <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-3888"></span>





<h2 class="wp-block-heading">The Asserts Hell</h2>



<p>Let&#8217;s consider the following unit test:</p>



<script src="https://gist.github.com/dsibinski/f8fea58079d68dd8ac0d194a33964e1b.js"></script>



<p>What&#8217;s wrong here? <em>Given</em> and <em>When</em> sections are great single-liners. We know straightaway what&#8217;s the input and the action executed. However, <em>Then</em> block is too complex. It&#8217;s hard to figure out, just passing quickly through this test, what is <em>expected</em>. This case is even not that bad thanks to the usage of <a href="https://fluentassertions.com/">FluentAssertions</a>.</p>



<p>I spend a lot of time writing tests (<a href="https://www.codejourney.net/2017/03/unit-testing-xamarin-application/">unit</a> or integration), but I spend even more time reading them. I always hope that I will find these <em>idyllic</em> tests that act as code documentation&#8230; <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9d0.png" alt="🧐" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>However, let&#8217;s go step by step. I recently took place in a <a href="https://smarttesting.pl/">software testing course</a>. That&#8217;s where I discovered the solution to <em>The Asserts Hell</em>.</p>



<h2 class="wp-block-heading">Assert Object for Better Asserts</h2>



<p>Assert Object pattern is what solves our problem. The idea is to create an <em>Assert</em> class which wraps the original object being tested. In our case the tested object is of <span style="color:#ff9f05" class="tadv-color">Product</span> class, so our assert class will be called <span style="color:#ff9f05" class="tadv-color">ProductAssert</span>.</p>



<p>Let&#8217;s see the implementation:</p>



<script src="https://gist.github.com/dsibinski/10a3c47a7e997eed29fabcb07914786b.js"></script>



<p>As you can see, we simply moved the asserts from our test&#8217;s Then section into <span style="color:#ff9f05" class="tadv-color">ProductAssert</span> class&#8217;s methods. Additionally, we always return <span style="color:#ff9f05" class="tadv-color">this</span> from each asserting method, which allows chaining functions calls.</p>



<p>We can now use such assert object<span style="color:#444" class="tadv-color"> in our</span> unit test: </p>



<script src="https://gist.github.com/dsibinski/e0a649e6134b9c6b389d3419db62f92a.js"></script>



<p>You must agree it&#8217;s much more readable now <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>We can of course improve it even more, for example by wrapping these 3 assertion methods (<span style="color:#ff9f05" class="tadv-color">BeAvailable()</span>, <span style="color:#ff9f05" class="tadv-color">HaveDiscount() </span><span style="color:#444" class="tadv-color">and</span><span style="color:#ff9f05" class="tadv-color"> Cost()</span>) into a single one. We can also easily create an <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods">extension method</a> for the <span style="color:#ff9f05" class="tadv-color">Product</span> class itself, so the assert object doesn&#8217;t have to be instantiated in our unit test. I could even add an extension to <em>FluentAssertions</em> library.</p>



<p>You can find complete source code used as examples in this article <a href="https://github.com/dsibinski/Playground/tree/main/Playground/Playground.Tests">here</a>.</p>



<h2 class="wp-block-heading">Assert Object &#8211; summary</h2>



<p>I personally find Assert Object a very useful tests code refactoring method. As a programmer, you spend most of your time not on writing, but on reading the source code. Clear and simple tests can make your (and your colleagues&#8217;) life much better <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Of course, the Assert Object pattern should not be your default way of asserting. If you can complete your test with one or two assertions, then it&#8217;s probably better to keep it without wrapping into any additional objects. Having too many <em>assert</em> statements might also mean that the objects being tested are poorly designed. However, as we often work with legacy code which we can&#8217;t easily change or refactor, it might be a good solution.</p>



<p>What patterns for writing better tests do you use? Share your tips in the comments!</p>
<p>The post <a href="https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/">Improve your tests with Assert Object Pattern</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3888</post-id>	</item>
	</channel>
</rss>
