<commit_before>---
layout: post
tags: [Productivity, Unit Testing]
date: 2009-03-16 20:13:29 UTC
title: "Fixture Object"
---
{% include JB/setup %}

<div id="post">
	<p>(A <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction TDD</a> post)</p> <p>For a simple API, setting up the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a> may be as simple as creating a new instance of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, and possibly any <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Expected</a> or <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a>. On the other hand, for a complex API, setting up the fixture may require quite a bit of (potentially repetitive) code.</p> <p>Since the DRY principle also applies to test code, it quickly becomes necessary to create test-specific helper methods and other <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20Encapsulation%20Method">SUT API Encapsulation</a>, and I've found that instead of creating a more or less unplanned set of disconnected helper methods, it's much cleaner (and, not to mention, much more object-oriented) to create a single object that represents the Fixture, and attach the helper methods to this object.</p> <p>Let's look at an example.</p> <p>Here's a unit test with a complex <a href="http://xunitpatterns.com/fixture%20setup.html">Fixture Setup</a>:</p><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 3,\par ??        Text = \cf6 "Anonymous text 1"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing2 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 6,\par ??        Text = \cf6 "Anonymous text 2"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing3 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 1,\par ??        Text = \cf6 "Anonymous text 3"\par ??\cf0     \};\par ??\par ??    \cf4 int\cf0  expectedSum = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \}.\par ??        Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fake.AddThing(thing1);\par ??    fake.AddThing(thing2);\par ??    fake.AddThing(thing3);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--> <div style="font-size: 10pt; background: white; color: black; font-family: courier new"><pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre><pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Naïve()</pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 3,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 1"</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; };</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 6,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 2"</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; };</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 1,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 3"</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; };</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = <span style="color: blue">new</span>[] { thing1, thing2, thing3 }.</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select(t =&gt; t.Number).Sum();</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; fake.AddThing(thing1);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; fake.AddThing(thing2);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; fake.AddThing(thing3);</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>If this was a truly one-off test and you know with certainty that there's going to be no other tests just remotely similar to this one, just hard-coding the entire Fixture Setup inline is in order, but as soon as the need for similar tests arises, this approach leads to repetitive code, and hence unmaintainable tests.</p>
<p>The more repetitive code that can be delegated to helper methods the better. A common refactoring of the previous test might then look something like this:</p><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Helpers()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0  thing2 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0  thing3 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0 [] things = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \};\par ??\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    \cf3 MyClassTest\cf0 .AddThingsToMyInterface(fake, things);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: courier new"><pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre><pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Helpers()</pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span>[] things = <span style="color: blue">new</span>[] { thing1, thing2, thing3 };</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = things.Select(t =&gt; t.Number).Sum();</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassTest</span>.AddThingsToMyInterface(fake, things);</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>While this is better, the helper methods are static methods, so it's necessary to pass too much state around. The array of Things and the fake is both needed in the test itself, as well as in the AddThingsToMyInterface helper method.</p>
<p>By moving and refactoring the helper methods to a new class that represents the Fixture, the test code becomes both more reusable and more readable.</p><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_FixtureObject()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddAnonymousThings();\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateSut();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: courier new"><pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre><pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_FixtureObject()</pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassFixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">MyClassFixture</span>();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; fixture.AddAnonymousThings();</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = </pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Things.Select(t =&gt; t.Number).Sum();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = fixture.CreateSut();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>The MyClassFixture instance now holds the state of the Fixture, so there's much less need to pass around as much data as before. The set of Things is now contained within the Fixture object itself, and the fake has totally disappeared from the test; it's still present, but now encapsulated within MyClassFixture.</p><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 class\cf0  \cf4 MyClassFixture\par ??\cf0 \{\par ??    \cf1 public\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 Thing\cf0 &gt;();\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 FakeMyInterface\cf0  Fake \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &lt;\cf4 Thing\cf0 &gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 internal\cf0  \cf1 void\cf0  AddAnonymousThings()\par ??    \{\par ??        \cf1 int\cf0  many = 3;\par ??        \cf1 for\cf0  (\cf1 int\cf0  i = 0; i &lt; many; i++)\par ??        \{\par ??            \cf4 Thing\cf0  t = \cf1 this\cf0 .CreateAnonymousThing();\par ??            \cf1 this\cf0 .Things.Add(t);\par ??            \cf1 this\cf0 .Fake.AddThing(t);\par ??        \}\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 MyClass\cf0  CreateSut()\par ??    \{\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (\cf1 this\cf0 .Fake);\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf4 Thing\cf0  CreateAnonymousThing()\par ??    \{\par ??        \cf4 Thing\cf0  t = \cf1 new\cf0  \cf4 Thing\cf0 ();\par ??        t.Number = \cf1 this\cf0 .Things.Count + 1;\par ??        t.Text = \cf4 Guid\cf0 .NewGuid().ToString();\par ??        \cf1 return\cf0  t;\par ??    \}\par ??\}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: courier new"><pre style="margin: 0px"><span style="color: blue">internal</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyClassFixture</span></pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> MyClassFixture()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Thing</span>&gt;();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">FakeMyInterface</span> Fake { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Thing</span>&gt; Things { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: blue">void</span> AddAnonymousThings()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> many = 3;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">for</span> (<span style="color: blue">int</span> i = 0; i &lt; many; i++)</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> t = <span style="color: blue">this</span>.CreateAnonymousThing();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things.Add(t);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Fake.AddThing(t);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">MyClass</span> CreateSut()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(<span style="color: blue">this</span>.Fake);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">Thing</span> CreateAnonymousThing()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> t = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Number = <span style="color: blue">this</span>.Things.Count + 1;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Text = <span style="color: #2b91af">Guid</span>.NewGuid().ToString();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> t;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">}</pre></div>
<p>The CreateAnonymousThing method uses <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained Non-Determinism</a> to create unique Thing instances. The AddAnonymousThings method uses <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 as an equivalence of many</a>, and the CreateSut method acts as a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT Factory</a>.</p>
<p>This is both more reusable and more expressive than a collection of disjointed static helper methods.</p>
<p>Whenever I begin to feel that setting up a Test Fixture is becoming too cumbersome, Fixture Object is the first pattern I consider.</p>
</div>
	<commit_msg>Removed redundant (and ugly) pre tags from a post.<commit_after>---
layout: post
tags: [Productivity, Unit Testing]
date: 2009-03-16 20:13:29 UTC
title: "Fixture Object"
---
{% include JB/setup %}

<div id="post">
	<p>(A <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction TDD</a> post)</p> <p>For a simple API, setting up the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a> may be as simple as creating a new instance of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, and possibly any <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Expected</a> or <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a>. On the other hand, for a complex API, setting up the fixture may require quite a bit of (potentially repetitive) code.</p> <p>Since the DRY principle also applies to test code, it quickly becomes necessary to create test-specific helper methods and other <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20Encapsulation%20Method">SUT API Encapsulation</a>, and I've found that instead of creating a more or less unplanned set of disconnected helper methods, it's much cleaner (and, not to mention, much more object-oriented) to create a single object that represents the Fixture, and attach the helper methods to this object.</p> <p>Let's look at an example.</p> <p>Here's a unit test with a complex <a href="http://xunitpatterns.com/fixture%20setup.html">Fixture Setup</a>:</p>
  <p><pre><div style="font-size: 10pt; color: black; font-family: courier new">[<span style="color: #2b91af">TestMethod</span>]
<span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Naïve()
{
&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span>
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 3,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 1"</span>
&nbsp;&nbsp;&nbsp; };
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 6,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 2"</span>
&nbsp;&nbsp;&nbsp; };
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 1,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 3"</span>
&nbsp;&nbsp;&nbsp; };
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = <span style="color: blue">new</span>[] { thing1, thing2, thing3 }.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select(t =&gt; t.Number).Sum();
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>();
&nbsp;&nbsp;&nbsp; fake.AddThing(thing1);
&nbsp;&nbsp;&nbsp; fake.AddThing(thing2);
&nbsp;&nbsp;&nbsp; fake.AddThing(thing3);
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake);
&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span>
&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings();
&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span>
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>);
&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span>
}</div></pre></p>
  <p>If this was a truly one-off test and you know with certainty that there's going to be no other tests just remotely similar to this one, just hard-coding the entire Fixture Setup inline is in order, but as soon as the need for similar tests arises, this approach leads to repetitive code, and hence unmaintainable tests.</p>
  <p>The more repetitive code that can be delegated to helper methods the better. A common refactoring of the previous test might then look something like this:</p>
  <p><pre><div style="font-size: 10pt; color: black; font-family: courier new">[<span style="color: #2b91af">TestMethod</span>]
<span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Helpers()
{
&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span>
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span>[] things = <span style="color: blue">new</span>[] { thing1, thing2, thing3 };
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = things.Select(t =&gt; t.Number).Sum();
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>();
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassTest</span>.AddThingsToMyInterface(fake, things);
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake);
&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span>
&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings();
&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span>
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>);
&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span>
}</div></pre></p>
  <p>While this is better, the helper methods are static methods, so it's necessary to pass too much state around. The array of Things and the fake is both needed in the test itself, as well as in the AddThingsToMyInterface helper method.</p>
  <p>By moving and refactoring the helper methods to a new class that represents the Fixture, the test code becomes both more reusable and more readable.</p>
  <p><pre><div style="font-size: 10pt; color: black; font-family: courier new">[<span style="color: #2b91af">TestMethod</span>]
<span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_FixtureObject()
{
&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span>
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassFixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">MyClassFixture</span>();
&nbsp;&nbsp;&nbsp; fixture.AddAnonymousThings();
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Things.Select(t =&gt; t.Number).Sum();
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = fixture.CreateSut();
&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span>
&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings();
&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span>
&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>);
&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span>
}</div></pre></p>
  <p>The MyClassFixture instance now holds the state of the Fixture, so there's much less need to pass around as much data as before. The set of Things is now contained within the Fixture object itself, and the fake has totally disappeared from the test; it's still present, but now encapsulated within MyClassFixture.</p>
  <p><pre><div style="font-size: 10pt; color: black; font-family: courier new"><span style="color: blue">internal</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyClassFixture</span>
{
&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> MyClassFixture()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Thing</span>&gt;();
&nbsp;&nbsp;&nbsp; }
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">FakeMyInterface</span> Fake { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Thing</span>&gt; Things { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: blue">void</span> AddAnonymousThings()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> many = 3;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">for</span> (<span style="color: blue">int</span> i = 0; i &lt; many; i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> t = <span style="color: blue">this</span>.CreateAnonymousThing();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things.Add(t);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Fake.AddThing(t);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">MyClass</span> CreateSut()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(<span style="color: blue">this</span>.Fake);
&nbsp;&nbsp;&nbsp; }
&nbsp;
&nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">Thing</span> CreateAnonymousThing()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> t = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Number = <span style="color: blue">this</span>.Things.Count + 1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Text = <span style="color: #2b91af">Guid</span>.NewGuid().ToString();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> t;
&nbsp;&nbsp;&nbsp; }
}</div></pre></p>
  <p>The CreateAnonymousThing method uses <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained Non-Determinism</a> to create unique Thing instances. The AddAnonymousThings method uses <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 as an equivalence of many</a>, and the CreateSut method acts as a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT Factory</a>.</p>
  <p>This is both more reusable and more expressive than a collection of disjointed static helper methods.</p>
  <p>Whenever I begin to feel that setting up a Test Fixture is becoming too cumbersome, Fixture Object is the first pattern I consider.</p>
</div>
	
{'fraction_non_alphanumeric': 0.2175478658001054, 'fraction_numerical': 0.03319866502722642, 'mean_word_length': 6.260148777895855, 'pattern_counts': {'":': 0, '<': 833, '<?xml version=': 0, '>': 833, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6595610', 'n_tokens_mistral': 12399, 'n_tokens_neox': 11627, 'n_words': 2638}