<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>ploeh blog - Unit Testing</title>
    <link>http://blog.ploeh.dk/</link>
    <description>Mark Seemann's .NET blog</description>
    <language>en-us</language>
    <copyright>Mark Seemann</copyright>
    <lastBuildDate>Thu, 19 Aug 2010 19:25:50 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mark@seemann.ms</managingEditor>
    <webMaster>mark@seemann.ms</webMaster>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2479066c-c462-43db-959b-6364792cd1f5</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2479066c-c462-43db-959b-6364792cd1f5.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2479066c-c462-43db-959b-6364792cd1f5.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2479066c-c462-43db-959b-6364792cd1f5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The new internal architecture of <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0
enables some interesting features. One of these is that it becomes easy to extend
AutoFixture to become an auto-mocking container.
</p>
        <p>
Since I personally use <a href="http://code.google.com/p/moq/">Moq</a>, the AutoFixture
2.0 .zip file includes a new assembly called Ploeh.AutoFixture.AutoMoq that includes
an auto-mocking extension that uses Moq for Test Doubles.
</p>
        <blockquote>
          <p>
Please note that AutoFixture in itself has no dependency on Moq. If you don’t want
to use Moq, you can just ignore the Ploeh.AutoFixture.AutoMoq assembly.
</p>
          <p>
Auto-mocking with AutoFixture does not have to use Moq. Although it only ships with
Moq support, it is possible to write an auto-mocking extension for a different dynamic
mock library.
</p>
        </blockquote>
        <p>
To use it, you must first add a reference to Ploeh.AutoFixture.AutoMoq. You can now
create your Fixture instance like this:
</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 var\cf0  fixture = \cf1 new\cf0  \cf4 Fixture\cf0 ()\par ??    .Customize(\cf1 new\cf0  \cf4 AutoMoqCustomization\cf0 ());}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>()</pre>
          <pre style="margin: 0px">    .Customize(<span style="color: blue">new</span><span style="color: #2b91af">AutoMoqCustomization</span>());</pre>
        </div>
        <p>
What this does is that it adds a fallback mechanism to the fixture. If a type falls
through the normal engine without being handled, the auto-mocking extension will check
whether it is a request for an interface or abstract class. If this is so, it will <em>relay</em> the
request to a request for a Mock of the same type.
</p>
        <p>
A different part of the extension handles requests for Mocks, which ensures that the
Mock will be created and returned.
</p>
        <p>
Splitting up auto-mocking into a relay and a creational strategy for Mock objects
proper also means that we can directly request a Mock if we would like that. Even
better, we can use the built-in <a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx">Freeze
support</a> to freeze a Mock, and it will also automatically freeze the auto-mocked
instance as well (because the relay will ask for a Mock that turns out to be frozen).
</p>
        <p>
Returning to the <a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx">original
frozen pizza example</a>, we can now rewrite it 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;}??\fs20 [\cf3 Fact\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ()\par ??        .Customize(\cf4 new\cf0  \cf3 AutoMoqCustomization\cf0 ());\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&lt;\cf3 Basket\cf0 &gt;();\par ??    \cf4 var\cf0  mapMock = fixture.Freeze&lt;\cf3 Mock\cf0 &lt;\cf3 IPizzaMap\cf0 &gt;&gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&lt;\cf3 PizzaPresenter\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 BasketPresenter\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">[<span style="color: #2b91af">Fact</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> AddWillPipeMapCorrectly()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>()</pre>
          <pre style="margin: 0px">        .Customize(<span style="color: blue">new</span><span style="color: #2b91af">AutoMoqCustomization</span>());</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> basket
= fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> mapMock
= fixture.Freeze&lt;<span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> pizza
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    sut.Add(pizza);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Notice that we can simply freeze Mock&lt;IPizzaMap&gt; which also automatically freeze
the IPizzaMap instance as well. When we later create the <a href="http://xunitpatterns.com/SUT.html">SUT</a> by
requesting an anonymous BasketPresenter, IPizzaMap is already frozen in the fixture,
so the correct instance will be injected into the SUT.
</p>
        <p>
This is similar to the behavior of the <a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx">custom
FreezeMoq extension method</a> I previously described, but now this feature is baked
in.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2479066c-c462-43db-959b-6364792cd1f5" />
      </body>
      <title>AutoFixture as an auto-mocking container</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2479066c-c462-43db-959b-6364792cd1f5.aspx</guid>
      <link>http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx</link>
      <pubDate>Thu, 19 Aug 2010 19:25:50 GMT</pubDate>
      <description>&lt;p&gt;
The new internal architecture of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 2.0
enables some interesting features. One of these is that it becomes easy to extend
AutoFixture to become an auto-mocking container.
&lt;/p&gt;
&lt;p&gt;
Since I personally use &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt;, the AutoFixture
2.0 .zip file includes a new assembly called Ploeh.AutoFixture.AutoMoq that includes
an auto-mocking extension that uses Moq for Test Doubles.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Please note that AutoFixture in itself has no dependency on Moq. If you don’t want
to use Moq, you can just ignore the Ploeh.AutoFixture.AutoMoq assembly.
&lt;/p&gt;
&lt;p&gt;
Auto-mocking with AutoFixture does not have to use Moq. Although it only ships with
Moq support, it is possible to write an auto-mocking extension for a different dynamic
mock library.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
To use it, you must first add a reference to Ploeh.AutoFixture.AutoMoq. You can now
create your Fixture instance like this:
&lt;/p&gt;
&lt;!--
{\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 var\cf0  fixture = \cf1 new\cf0  \cf4 Fixture\cf0 ()\par ??    .Customize(\cf1 new\cf0  \cf4 AutoMoqCustomization\cf0 ());}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AutoMoqCustomization&lt;/span&gt;());&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
What this does is that it adds a fallback mechanism to the fixture. If a type falls
through the normal engine without being handled, the auto-mocking extension will check
whether it is a request for an interface or abstract class. If this is so, it will &lt;em&gt;relay&lt;/em&gt; the
request to a request for a Mock of the same type.
&lt;/p&gt;
&lt;p&gt;
A different part of the extension handles requests for Mocks, which ensures that the
Mock will be created and returned.
&lt;/p&gt;
&lt;p&gt;
Splitting up auto-mocking into a relay and a creational strategy for Mock objects
proper also means that we can directly request a Mock if we would like that. Even
better, we can use the built-in &lt;a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx"&gt;Freeze
support&lt;/a&gt; to freeze a Mock, and it will also automatically freeze the auto-mocked
instance as well (because the relay will ask for a Mock that turns out to be frozen).
&lt;/p&gt;
&lt;p&gt;
Returning to the &lt;a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx"&gt;original
frozen pizza example&lt;/a&gt;, we can now rewrite it like this:
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 Fact\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ()\par ??        .Customize(\cf4 new\cf0  \cf3 AutoMoqCustomization\cf0 ());\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&amp;lt;\cf3 Basket\cf0 &amp;gt;();\par ??    \cf4 var\cf0  mapMock = fixture.Freeze&amp;lt;\cf3 Mock\cf0 &amp;lt;\cf3 IPizzaMap\cf0 &amp;gt;&amp;gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&amp;lt;\cf3 PizzaPresenter\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 BasketPresenter\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     mapMock.Verify(m =&amp;gt; m.Pipe(pizza, basket.Add));\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;Fact&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddWillPipeMapCorrectly()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AutoMoqCustomization&lt;/span&gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; basket
= fixture.Freeze&amp;lt;&lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; mapMock
= fixture.Freeze&amp;lt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; pizza
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;BasketPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Add(pizza);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMock.Verify(m =&amp;gt; m.Pipe(pizza, basket.Add));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Notice that we can simply freeze Mock&amp;lt;IPizzaMap&amp;gt; which also automatically freeze
the IPizzaMap instance as well. When we later create the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; by
requesting an anonymous BasketPresenter, IPizzaMap is already frozen in the fixture,
so the correct instance will be injected into the SUT.
&lt;/p&gt;
&lt;p&gt;
This is similar to the behavior of the &lt;a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx"&gt;custom
FreezeMoq extension method&lt;/a&gt; I previously described, but now this feature is baked
in.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2479066c-c462-43db-959b-6364792cd1f5" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2479066c-c462-43db-959b-6364792cd1f5.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1e9b836b-0557-4981-ab24-9e863268fa81</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1e9b836b-0557-4981-ab24-9e863268fa81.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1e9b836b-0557-4981-ab24-9e863268fa81.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1e9b836b-0557-4981-ab24-9e863268fa81</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The <a href="http://blog.ploeh.dk/2010/04/06/MappingTypesWithAutoFixture.aspx">last
time I presented a sample</a> of an <a href="http://autofixture.codeplex.com/">AutoFixture</a>-based
unit test, I purposely glossed over the state-based verification that asserted that
the resulting state of the <em>basket</em> variable was that the appropriate Pizza
was added:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .IsTrue(basket.Pizze.Any(p =&gt;\par ??    p.Name == pizza.Name), \cf4 "Basket has added pizza."\cf0 );}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(p
=&gt;</pre>
          <pre style="margin: 0px">    p.Name == pizza.Name), <span style="color: #a31515">"Basket
has added pizza."</span>);</pre>
        </div>
        <p>
The main issue with this assertion is that the implied equality expression is rather
weak: we consider a PizzaPresenter instance to be equal to a Pizza instance if their
Name properties match.
</p>
        <p>
What if they have other properties (like Size) that don’t match? If this is the case,
the test would be a <a href="http://xunitpatterns.com/false%20negative.html">false
negative</a>. A match would be found in the Pizze collection, but the instances would
not truly represent the same pizza.
</p>
        <p>
How do we resolve this conundrum without introducing <a href="http://xunitpatterns.com/Test%20Logic%20in%20Production.html#Equality%20Pollution">equality
pollution</a>? AutoFixture offers one option in the form of the generic Likeness&lt;TSource,
TDestination&gt; class. This class offers convention-based <a href="http://xunitpatterns.com/test-specific%20equality.html">test-specific
equality</a> mapping from TSource to TDestination and overriding the Equals method.
</p>
        <p>
One of the ways we can use it is by a convenience extension method. This unit test
is a refactoring of the test from the previous post, but now using Likeness:
</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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillAddToBasket_Likeness()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    fixture.Register&lt;\cf3 IPizzaMap\cf0 , \cf3 PizzaMap\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&lt;\cf3 Basket\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&lt;\cf3 PizzaPresenter\cf0 &gt;();\par ??    \cf4 var\cf0  expectedPizza = \par ??        pizza.AsSource().OfLikeness&lt;\cf3 Pizza\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 BasketPresenter\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .IsTrue(basket.Pizze.Any(expectedPizza.Equals));\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> AddWillAddToBasket_Likeness()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>, <span style="color: #2b91af">PizzaMap</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> basket
= fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> pizza
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> expectedPizza
= </pre>
          <pre style="margin: 0px">        pizza.AsSource().OfLikeness&lt;<span style="color: #2b91af">Pizza</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    sut.Add(pizza);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(expectedPizza.Equals));</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Notice how the Likeness instance is created with the AsSource() extension method.
The <em>pizza</em> instance (of type PizzaPresenter) is the source of the Likeness,
whereas the Pizza domain model type is the destination. The <em>expectedPizza</em> instance
is of type Likeness&lt;PizzaPresenter, Pizza&gt;.
</p>
        <p>
The Likeness class overrides Equals with a convention-based comparison: if two properties
have the same name and type, they are equal if their values are equal. All public
properties on the destination must have equal properties on the source.
</p>
        <p>
This allows me to specify the Equals method as the predicate for the Any method in
the assertion:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 Assert\cf0 .IsTrue(basket.Pizze.Any(expectedPizza.Equals));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(expectedPizza.Equals));</pre>
        </div>
        <p>
When the Any method evalues the Pizze collection, it executes the Equals method on
Likeness, resulting in a convention-based comparison of all public properties and
fields on the two instances.
</p>
        <p>
It’s possible to customize the comparison to override the behavior for certain properties,
but I will leave that to later posts. This post only scratches the surface of what
Likeness can do.
</p>
        <p>
To use Likeness, you must add a reference to the Ploeh.SemanticComparison assembly.
You can create a new instance using the public constructor, but to use the AsSource
extension method, you will need to add a using directive:
</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;}??\fs20 \cf1 using\cf0  Ploeh.SemanticComparison.Fluent;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">using</span> Ploeh.SemanticComparison.Fluent;</pre>
        </div>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1e9b836b-0557-4981-ab24-9e863268fa81" />
      </body>
      <title>Introducing AutoFixture Likeness</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1e9b836b-0557-4981-ab24-9e863268fa81.aspx</guid>
      <link>http://blog.ploeh.dk/2010/06/29/IntroducingAutoFixtureLikeness.aspx</link>
      <pubDate>Tue, 29 Jun 2010 06:39:30 GMT</pubDate>
      <description>&lt;p&gt;
The &lt;a href="http://blog.ploeh.dk/2010/04/06/MappingTypesWithAutoFixture.aspx"&gt;last
time I presented a sample&lt;/a&gt; of an &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;-based
unit test, I purposely glossed over the state-based verification that asserted that
the resulting state of the &lt;em&gt;basket&lt;/em&gt; variable was that the appropriate Pizza
was added:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .IsTrue(basket.Pizze.Any(p =&amp;gt;\par ??    p.Name == pizza.Name), \cf4 "Basket has added pizza."\cf0 );}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(basket.Pizze.Any(p
=&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Name == pizza.Name), &lt;span style="color: #a31515"&gt;"Basket
has added pizza."&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The main issue with this assertion is that the implied equality expression is rather
weak: we consider a PizzaPresenter instance to be equal to a Pizza instance if their
Name properties match.
&lt;/p&gt;
&lt;p&gt;
What if they have other properties (like Size) that don’t match? If this is the case,
the test would be a &lt;a href="http://xunitpatterns.com/false%20negative.html"&gt;false
negative&lt;/a&gt;. A match would be found in the Pizze collection, but the instances would
not truly represent the same pizza.
&lt;/p&gt;
&lt;p&gt;
How do we resolve this conundrum without introducing &lt;a href="http://xunitpatterns.com/Test%20Logic%20in%20Production.html#Equality%20Pollution"&gt;equality
pollution&lt;/a&gt;? AutoFixture offers one option in the form of the generic Likeness&amp;lt;TSource,
TDestination&amp;gt; class. This class offers convention-based &lt;a href="http://xunitpatterns.com/test-specific%20equality.html"&gt;test-specific
equality&lt;/a&gt; mapping from TSource to TDestination and overriding the Equals method.
&lt;/p&gt;
&lt;p&gt;
One of the ways we can use it is by a convenience extension method. This unit test
is a refactoring of the test from the previous post, but now using Likeness:
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillAddToBasket_Likeness()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    fixture.Register&amp;lt;\cf3 IPizzaMap\cf0 , \cf3 PizzaMap\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&amp;lt;\cf3 Basket\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&amp;lt;\cf3 PizzaPresenter\cf0 &amp;gt;();\par ??    \cf4 var\cf0  expectedPizza = \par ??        pizza.AsSource().OfLikeness&amp;lt;\cf3 Pizza\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 BasketPresenter\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .IsTrue(basket.Pizze.Any(expectedPizza.Equals));\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddWillAddToBasket_Likeness()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;PizzaMap&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; basket
= fixture.Freeze&amp;lt;&lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; pizza
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; expectedPizza
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pizza.AsSource().OfLikeness&amp;lt;&lt;span style="color: #2b91af"&gt;Pizza&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;BasketPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Add(pizza);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(basket.Pizze.Any(expectedPizza.Equals));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Notice how the Likeness instance is created with the AsSource() extension method.
The &lt;em&gt;pizza&lt;/em&gt; instance (of type PizzaPresenter) is the source of the Likeness,
whereas the Pizza domain model type is the destination. The &lt;em&gt;expectedPizza&lt;/em&gt; instance
is of type Likeness&amp;lt;PizzaPresenter, Pizza&amp;gt;.
&lt;/p&gt;
&lt;p&gt;
The Likeness class overrides Equals with a convention-based comparison: if two properties
have the same name and type, they are equal if their values are equal. All public
properties on the destination must have equal properties on the source.
&lt;/p&gt;
&lt;p&gt;
This allows me to specify the Equals method as the predicate for the Any method in
the assertion:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 Assert\cf0 .IsTrue(basket.Pizze.Any(expectedPizza.Equals));}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(basket.Pizze.Any(expectedPizza.Equals));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
When the Any method evalues the Pizze collection, it executes the Equals method on
Likeness, resulting in a convention-based comparison of all public properties and
fields on the two instances.
&lt;/p&gt;
&lt;p&gt;
It’s possible to customize the comparison to override the behavior for certain properties,
but I will leave that to later posts. This post only scratches the surface of what
Likeness can do.
&lt;/p&gt;
&lt;p&gt;
To use Likeness, you must add a reference to the Ploeh.SemanticComparison assembly.
You can create a new instance using the public constructor, but to use the AsSource
extension method, you will need to add a using directive:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 using\cf0  Ploeh.SemanticComparison.Fluent;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; Ploeh.SemanticComparison.Fluent;&lt;/pre&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1e9b836b-0557-4981-ab24-9e863268fa81" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1e9b836b-0557-4981-ab24-9e863268fa81.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=83ccb1fd-21ad-4fd5-bf0b-e61d321f7991</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,83ccb1fd-21ad-4fd5-bf0b-e61d321f7991.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,83ccb1fd-21ad-4fd5-bf0b-e61d321f7991.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=83ccb1fd-21ad-4fd5-bf0b-e61d321f7991</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the next couple of weeks I will be giving a couple of talks in Copenhagen.
</p>
        <p>
At <a href="http://communityday.in/copenhagen/Home/Agenda">Community Day 2010</a> I
will be giving two talks on respectively Dependency Injection and TDD.
</p>
        <p>
In early June I will be giving a <a href="http://www.eventbrite.com/event/660219735">repeat
of my previous CNUG TDD talk</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=83ccb1fd-21ad-4fd5-bf0b-e61d321f7991" />
      </body>
      <title>Upcoming talks spring 2010</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,83ccb1fd-21ad-4fd5-bf0b-e61d321f7991.aspx</guid>
      <link>http://blog.ploeh.dk/2010/05/23/UpcomingTalksSpring2010.aspx</link>
      <pubDate>Sun, 23 May 2010 16:11:37 GMT</pubDate>
      <description>&lt;p&gt;
In the next couple of weeks I will be giving a couple of talks in Copenhagen.
&lt;/p&gt;
&lt;p&gt;
At &lt;a href="http://communityday.in/copenhagen/Home/Agenda"&gt;Community Day 2010&lt;/a&gt; I
will be giving two talks on respectively Dependency Injection and TDD.
&lt;/p&gt;
&lt;p&gt;
In early June I will be giving a &lt;a href="http://www.eventbrite.com/event/660219735"&gt;repeat
of my previous CNUG TDD talk&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=83ccb1fd-21ad-4fd5-bf0b-e61d321f7991" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,83ccb1fd-21ad-4fd5-bf0b-e61d321f7991.aspx</comments>
      <category>Dependency Injection</category>
      <category>Miscellaneous</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=589220df-9778-41a9-a7df-e0543901bb2b</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,589220df-9778-41a9-a7df-e0543901bb2b.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,589220df-9778-41a9-a7df-e0543901bb2b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=589220df-9778-41a9-a7df-e0543901bb2b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.manning.com/seemann/">My book</a> contains a section on the <a href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx">Ambient
Context</a> pattern that uses a TimeProvider as an example. It’s used like this:
</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 this\cf0 .closedAt = \cf4 TimeProvider\cf0 .Current.UtcNow;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">this</span>.closedAt
= <span style="color: #2b91af">TimeProvider</span>.Current.UtcNow;</pre>
        </div>
        <p>
Yesterday I was TDDing a state machine that consumes TimeProvider and needed to freeze
and advance time at different places in the test. Always on the lookout for making
unit tests more readable, I decided to have a little fun with literal extensions and
TimeProvider. I ended up with this test:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green128\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 // Fixture setup\par ??\cf3 var\cf0  fixture = \cf3 new\cf0  \cf5 WcfFixture\cf0 ();\par ??\par ??\cf5 DateTime\cf0 .Now.Freeze();\par ??\par ??fixture.Register(1.Minutes());\par ??\cf3 var\cf0  sut = fixture.CreateAnonymous&lt;\cf5 CircuitBreaker\cf0 &gt;();\par ??sut.PutInOpenState();\par ??\par ??2.Minutes().Pass();\par ??\cf1 // Exercise system\par ??\cf0 sut.Guard();\par ??\cf1 // Verify outcome\par ??\cf5 Assert\cf0 .IsInstanceOfType(sut.State,\par ??    \cf3 typeof\cf0 (\cf5 HalfOpenCircuitState\cf0 ));\par ??\cf1 // Teardown}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: green">//
Fixture setup</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">WcfFixture</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: #2b91af">DateTime</span>.Now.Freeze();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">fixture.Register(1.Minutes());</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">CircuitBreaker</span>&gt;();</pre>
          <pre style="margin: 0px">sut.PutInOpenState();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">2.Minutes().Pass();</pre>
          <pre style="margin: 0px">
            <span style="color: green">//
Exercise system</span>
          </pre>
          <pre style="margin: 0px">sut.Guard();</pre>
          <pre style="margin: 0px">
            <span style="color: green">//
Verify outcome</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #2b91af">Assert</span>.IsInstanceOfType(sut.State,</pre>
          <pre style="margin: 0px">    <span style="color: blue">typeof</span>(<span style="color: #2b91af">HalfOpenCircuitState</span>));</pre>
          <pre style="margin: 0px">
            <span style="color: green">//
Teardown</span>
          </pre>
        </div>
        <p>
There are several items of note. Imagine that we can freeze time!
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 DateTime\cf0 .Now.Freeze();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: #2b91af">DateTime</span>.Now.Freeze();</pre>
        </div>
        <p>
With the TimeProvider and an extension method, we can:
</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 static\cf0  \cf1 void\cf0  Freeze(\cf1 this\cf0  \cf4 DateTime\cf0  dt)\par ??\{\par ??    \cf1 var\cf0  timeProviderStub = \cf1 new\cf0  \cf4 Mock\cf0 &lt;\cf4 TimeProvider\cf0 &gt;();\par ??    timeProviderStub.SetupGet(tp =&gt; tp.UtcNow).Returns(dt);\par ??    \cf4 TimeProvider\cf0 .Current = timeProviderStub.Object;\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">static</span>
            <span style="color: blue">void</span> Freeze(<span style="color: blue">this</span><span style="color: #2b91af">DateTime</span> dt)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> timeProviderStub
= <span style="color: blue">new</span><span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">TimeProvider</span>&gt;();</pre>
          <pre style="margin: 0px">    timeProviderStub.SetupGet(tp =&gt; tp.UtcNow).Returns(dt);</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">TimeProvider</span>.Current
= timeProviderStub.Object;</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
This effectively sets up the TimeProvider to always return the same time.
</p>
        <p>
Later in the test I state that 2 minutes pass:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;}??\fs20 2.Minutes().Pass();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">2.Minutes().Pass();</pre>
        </div>
        <p>
I particularly like the grammatically correct English. This is accomplished with a
combination of a literal extension and changing the state of TimeProvider.
</p>
        <p>
First, the literal extension:
</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 static\cf0  \cf4 TimeSpan\cf0  Minutes(\cf1 this\cf0  \cf1 int\cf0  m)\par ??\{\par ??    \cf1 return\cf0  \cf4 TimeSpan\cf0 .FromMinutes(m);\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">static</span>
            <span style="color: #2b91af">TimeSpan</span> Minutes(<span style="color: blue">this</span><span style="color: blue">int</span> m)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">return</span><span style="color: #2b91af">TimeSpan</span>.FromMinutes(m);</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Given the TimeSpan returned from the Minutes method, I can now invoke the Pass extension
method:
</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 static\cf0  \cf1 void\cf0  Pass(\cf1 this\cf0  \cf4 TimeSpan\cf0  ts)\par ??\{\par ??    \cf1 var\cf0  previousTime = \cf4 TimeProvider\cf0 .Current.UtcNow;\par ??    (previousTime + ts).Freeze();\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">static</span>
            <span style="color: blue">void</span> Pass(<span style="color: blue">this</span><span style="color: #2b91af">TimeSpan</span> ts)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> previousTime
= <span style="color: #2b91af">TimeProvider</span>.Current.UtcNow;</pre>
          <pre style="margin: 0px">    (previousTime + ts).Freeze();</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Note that I just add the TimeSpan to the current time and invoke the Freeze extension
method with the new value.
</p>
        <p>
Last, but not least, I should point out that the PutInOpenState method isn’t some
smelly test-specific method on the <a href="http://xunitpatterns.com/SUT.html">SUT</a>,
but rather yet another extension method.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=589220df-9778-41a9-a7df-e0543901bb2b" />
      </body>
      <title>Fun with literal extensions and Ambient Context</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,589220df-9778-41a9-a7df-e0543901bb2b.aspx</guid>
      <link>http://blog.ploeh.dk/2010/04/27/FunWithLiteralExtensionsAndAmbientContext.aspx</link>
      <pubDate>Tue, 27 Apr 2010 04:24:25 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.manning.com/seemann/"&gt;My book&lt;/a&gt; contains a section on the &lt;a href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx"&gt;Ambient
Context&lt;/a&gt; pattern that uses a TimeProvider as an example. It’s used like this:
&lt;/p&gt;
&lt;!--
{\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 this\cf0 .closedAt = \cf4 TimeProvider\cf0 .Current.UtcNow;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;.closedAt
= &lt;span style="color: #2b91af"&gt;TimeProvider&lt;/span&gt;.Current.UtcNow;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Yesterday I was TDDing a state machine that consumes TimeProvider and needed to freeze
and advance time at different places in the test. Always on the lookout for making
unit tests more readable, I decided to have a little fun with literal extensions and
TimeProvider. I ended up with this test:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green128\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 // Fixture setup\par ??\cf3 var\cf0  fixture = \cf3 new\cf0  \cf5 WcfFixture\cf0 ();\par ??\par ??\cf5 DateTime\cf0 .Now.Freeze();\par ??\par ??fixture.Register(1.Minutes());\par ??\cf3 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf5 CircuitBreaker\cf0 &amp;gt;();\par ??sut.PutInOpenState();\par ??\par ??2.Minutes().Pass();\par ??\cf1 // Exercise system\par ??\cf0 sut.Guard();\par ??\cf1 // Verify outcome\par ??\cf5 Assert\cf0 .IsInstanceOfType(sut.State,\par ??    \cf3 typeof\cf0 (\cf5 HalfOpenCircuitState\cf0 ));\par ??\cf1 // Teardown}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WcfFixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.Freeze();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.Register(1.Minutes());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;CircuitBreaker&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;sut.PutInOpenState();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;2.Minutes().Pass();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;sut.Guard();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsInstanceOfType(sut.State,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;HalfOpenCircuitState&lt;/span&gt;));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
There are several items of note. Imagine that we can freeze time!
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 DateTime\cf0 .Now.Freeze();}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.Freeze();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
With the TimeProvider and an extension method, we can:
&lt;/p&gt;
&lt;!--
{\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 static\cf0  \cf1 void\cf0  Freeze(\cf1 this\cf0  \cf4 DateTime\cf0  dt)\par ??\{\par ??    \cf1 var\cf0  timeProviderStub = \cf1 new\cf0  \cf4 Mock\cf0 &amp;lt;\cf4 TimeProvider\cf0 &amp;gt;();\par ??    timeProviderStub.SetupGet(tp =&amp;gt; tp.UtcNow).Returns(dt);\par ??    \cf4 TimeProvider\cf0 .Current = timeProviderStub.Object;\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Freeze(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; dt)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; timeProviderStub
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;TimeProvider&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; timeProviderStub.SetupGet(tp =&amp;gt; tp.UtcNow).Returns(dt);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;TimeProvider&lt;/span&gt;.Current
= timeProviderStub.Object;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This effectively sets up the TimeProvider to always return the same time.
&lt;/p&gt;
&lt;p&gt;
Later in the test I state that 2 minutes pass:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;}??\fs20 2.Minutes().Pass();}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;2.Minutes().Pass();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
I particularly like the grammatically correct English. This is accomplished with a
combination of a literal extension and changing the state of TimeProvider.
&lt;/p&gt;
&lt;p&gt;
First, the literal extension:
&lt;/p&gt;
&lt;!--
{\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 static\cf0  \cf4 TimeSpan\cf0  Minutes(\cf1 this\cf0  \cf1 int\cf0  m)\par ??\{\par ??    \cf1 return\cf0  \cf4 TimeSpan\cf0 .FromMinutes(m);\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt; Minutes(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; m)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;.FromMinutes(m);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Given the TimeSpan returned from the Minutes method, I can now invoke the Pass extension
method:
&lt;/p&gt;
&lt;!--
{\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 static\cf0  \cf1 void\cf0  Pass(\cf1 this\cf0  \cf4 TimeSpan\cf0  ts)\par ??\{\par ??    \cf1 var\cf0  previousTime = \cf4 TimeProvider\cf0 .Current.UtcNow;\par ??    (previousTime + ts).Freeze();\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Pass(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt; ts)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; previousTime
= &lt;span style="color: #2b91af"&gt;TimeProvider&lt;/span&gt;.Current.UtcNow;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (previousTime + ts).Freeze();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Note that I just add the TimeSpan to the current time and invoke the Freeze extension
method with the new value.
&lt;/p&gt;
&lt;p&gt;
Last, but not least, I should point out that the PutInOpenState method isn’t some
smelly test-specific method on the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;,
but rather yet another extension method.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=589220df-9778-41a9-a7df-e0543901bb2b" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,589220df-9778-41a9-a7df-e0543901bb2b.aspx</comments>
      <category>Dependency Injection</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=6cdc7e45-ad80-4766-ba8f-cc4d82752572</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,6cdc7e45-ad80-4766-ba8f-cc4d82752572.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,6cdc7e45-ad80-4766-ba8f-cc4d82752572.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=6cdc7e45-ad80-4766-ba8f-cc4d82752572</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
About a month ago I decided to migrate from MSTest to <a href="http://xunit.codeplex.com/wikipage">xUnit.net</a>,
and while I am still in the process, I haven’t regretted it yet, and I don’t expect
to. <a href="http://autofixture.codeplex.com/">AutoFixture</a> has already moved over,
and I’m slowly migrating all the sample code for <a href="http://www.manning.com/seemann/">my
book</a>.
</p>
        <p>
Recently I was asked <em>why</em>, which prompted me to write this post.
</p>
        <p>
I’m not moving away from MSTest for one single reason. It’s rather like lots of small
reasons.
</p>
        <p>
When I originally started out with TDD, I used nUnit – it was more or less the only
unit testing framework available for .NET at the time. When MSTest came, the change
was natural, since I worked for Microsoft at the time. This is not the case anymore,
but it still took me most of a year to finally abandon MSTest.
</p>
        <p>
There was one thing that really made me cling to MSTest, and that was the IDE integration,
but over time, I started to realize that this was the <em>only</em> reason, and even
that was getting flaky.
</p>
        <p>
When I started to think about all the things that left me dissatisfied, making the
decision was easy:
</p>
        <ul>
          <li>
First of all, MSTest isn’t extensible, but xUnit.net is. In xUnit.net, I can extend
the Fact or Theory attributes (and I intent to), while in MSTest, I will have to play
with the cards I’ve been dealt. I think I could live with all the other issues if
I could just have this one, but no. 
</li>
          <li>
MSTest has no support for parameterized test. xUnit.net does (via the Theory attribute). 
</li>
          <li>
MSTest has no Assert.Throws, although I <a href="https://connect.microsoft.com/VisualStudio/feedback/details/381288/assert-throws-for-mstest">requested
this feature a long time ago</a>. Now Visual Studio 2010 is out, but Assert.Throws
is still nowhere in sight. 
</li>
          <li>
MSTest has no x64 support. Tests always run as x86. Usually it’s no big deal, but
sometimes it’s a really big deal. 
</li>
          <li>
In MSTest, to write unit tests, you must create a special <em>Unit Test Project</em>,
and those are only available for C# and VB.net. Good luck trying to write unit tests
in a more exotic .NET language (like F# on Visual Studio 2008). xUnit.net doesn’t
have this problem. 
</li>
          <li>
MSTest uses Test Lists and .vsmdi files to maintain test lists. Why? I don’t care,
I just want to execute my tests, and the .vsmdi files are in the way. This is particularly
bad when you use TFS, but I’m also moving away from TFS, so that wouldn’t have continued
to be that much of an issue. Still: try having more than one .sln file with unit tests
in the same folder, and watch funny things happen because they need to share the same
.vsmdi file. 
</li>
          <li>
I suppose it’s because of the .vsmdi files, but sometimes I get a Test run error if
I delete a test and run the tests immediately after. That’s a false positive, if anyone
cares. 
</li>
          <li>
MSTest gives special treatment to its own AssertionException, which gets nice formatting
in the Test Results window. All other exceptions (like verification exceptions thrown
by Moq or Rhino Mocks are rendered near-intelligible because MSTest thinks it’s very
important to report the fully qualified name of the exception before its message.
Most of the time, you have to open the Test Details window to see the exception message. 
</li>
          <li>
Last, but not least, I often get cryptic exception messages like this one: <em>Column
'id_column, runid_column' is constrained to be unique.  Value '8c84fa94-04c1-424b-9868-57a2d4851a1d,
d7471c5e-522f-43d3-b2c5-8f5cab55af0e' is already present.</em> This appears in a very
annoying modal MessageBox, but clicking OK and retrying usually works, although sometimes
it even takes two or three attempts before I can get past this error.</li>
        </ul>
        <p>
It not one big thing, it’s just a lot of small, but very annoying things. After three
iterations (VS2005, VS2008 and now VS2010) these issue have still to be addressed,
and I got tired of waiting.
</p>
        <p>
So far, I can only say that I have none of these problems with xUnit.net and the IDE
integration provided by <a href="http://www.testdriven.net/">TestDriven.NET</a>. It’s
just a much smoother experience with much less friction.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=6cdc7e45-ad80-4766-ba8f-cc4d82752572" />
      </body>
      <title>Why I’m migrating from MSTest to xUnit.net</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,6cdc7e45-ad80-4766-ba8f-cc4d82752572.aspx</guid>
      <link>http://blog.ploeh.dk/2010/04/26/WhyImMigratingFromMSTestToXUnitnet.aspx</link>
      <pubDate>Mon, 26 Apr 2010 04:30:49 GMT</pubDate>
      <description>&lt;p&gt;
About a month ago I decided to migrate from MSTest to &lt;a href="http://xunit.codeplex.com/wikipage"&gt;xUnit.net&lt;/a&gt;,
and while I am still in the process, I haven’t regretted it yet, and I don’t expect
to. &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; has already moved over,
and I’m slowly migrating all the sample code for &lt;a href="http://www.manning.com/seemann/"&gt;my
book&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Recently I was asked &lt;em&gt;why&lt;/em&gt;, which prompted me to write this post.
&lt;/p&gt;
&lt;p&gt;
I’m not moving away from MSTest for one single reason. It’s rather like lots of small
reasons.
&lt;/p&gt;
&lt;p&gt;
When I originally started out with TDD, I used nUnit – it was more or less the only
unit testing framework available for .NET at the time. When MSTest came, the change
was natural, since I worked for Microsoft at the time. This is not the case anymore,
but it still took me most of a year to finally abandon MSTest.
&lt;/p&gt;
&lt;p&gt;
There was one thing that really made me cling to MSTest, and that was the IDE integration,
but over time, I started to realize that this was the &lt;em&gt;only&lt;/em&gt; reason, and even
that was getting flaky.
&lt;/p&gt;
&lt;p&gt;
When I started to think about all the things that left me dissatisfied, making the
decision was easy:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
First of all, MSTest isn’t extensible, but xUnit.net is. In xUnit.net, I can extend
the Fact or Theory attributes (and I intent to), while in MSTest, I will have to play
with the cards I’ve been dealt. I think I could live with all the other issues if
I could just have this one, but no. 
&lt;li&gt;
MSTest has no support for parameterized test. xUnit.net does (via the Theory attribute). 
&lt;li&gt;
MSTest has no Assert.Throws, although I &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/details/381288/assert-throws-for-mstest"&gt;requested
this feature a long time ago&lt;/a&gt;. Now Visual Studio 2010 is out, but Assert.Throws
is still nowhere in sight. 
&lt;li&gt;
MSTest has no x64 support. Tests always run as x86. Usually it’s no big deal, but
sometimes it’s a really big deal. 
&lt;li&gt;
In MSTest, to write unit tests, you must create a special &lt;em&gt;Unit Test Project&lt;/em&gt;,
and those are only available for C# and VB.net. Good luck trying to write unit tests
in a more exotic .NET language (like F# on Visual Studio 2008). xUnit.net doesn’t
have this problem. 
&lt;li&gt;
MSTest uses Test Lists and .vsmdi files to maintain test lists. Why? I don’t care,
I just want to execute my tests, and the .vsmdi files are in the way. This is particularly
bad when you use TFS, but I’m also moving away from TFS, so that wouldn’t have continued
to be that much of an issue. Still: try having more than one .sln file with unit tests
in the same folder, and watch funny things happen because they need to share the same
.vsmdi file. 
&lt;li&gt;
I suppose it’s because of the .vsmdi files, but sometimes I get a Test run error if
I delete a test and run the tests immediately after. That’s a false positive, if anyone
cares. 
&lt;li&gt;
MSTest gives special treatment to its own AssertionException, which gets nice formatting
in the Test Results window. All other exceptions (like verification exceptions thrown
by Moq or Rhino Mocks are rendered near-intelligible because MSTest thinks it’s very
important to report the fully qualified name of the exception before its message.
Most of the time, you have to open the Test Details window to see the exception message. 
&lt;li&gt;
Last, but not least, I often get cryptic exception messages like this one: &lt;em&gt;Column
'id_column, runid_column' is constrained to be unique.&amp;nbsp; Value '8c84fa94-04c1-424b-9868-57a2d4851a1d,
d7471c5e-522f-43d3-b2c5-8f5cab55af0e' is already present.&lt;/em&gt; This appears in a very
annoying modal MessageBox, but clicking OK and retrying usually works, although sometimes
it even takes two or three attempts before I can get past this error.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
It not one big thing, it’s just a lot of small, but very annoying things. After three
iterations (VS2005, VS2008 and now VS2010) these issue have still to be addressed,
and I got tired of waiting.
&lt;/p&gt;
&lt;p&gt;
So far, I can only say that I have none of these problems with xUnit.net and the IDE
integration provided by &lt;a href="http://www.testdriven.net/"&gt;TestDriven.NET&lt;/a&gt;. It’s
just a much smoother experience with much less friction.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=6cdc7e45-ad80-4766-ba8f-cc4d82752572" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,6cdc7e45-ad80-4766-ba8f-cc4d82752572.aspx</comments>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=a19c5021-1467-40fe-b0fd-c276fde0713e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,a19c5021-1467-40fe-b0fd-c276fde0713e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,a19c5021-1467-40fe-b0fd-c276fde0713e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=a19c5021-1467-40fe-b0fd-c276fde0713e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx">previous</a><a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx">posts</a> I
demonstrated interaction-based unit tests that verify that a pizza is correctly being
added to a shopping basket. An alternative is a state-based test where we examine
the contents of the shopping basket after exercising the <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
Here’s an initial attempt:
</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  AddWillAddToBasket()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    fixture.Register&lt;\cf3 IPizzaMap\cf0 &gt;(\par ??        fixture.CreateAnonymous&lt;\cf3 PizzaMap\cf0 &gt;);\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&lt;\cf3 Basket\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&lt;\cf3 PizzaPresenter\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 BasketPresenter\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .IsTrue(basket.Pizze.Any(p =&gt; \par ??        p.Name == pizza.Name), \cf6 "Basket has added pizza."\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> AddWillAddToBasket()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;(</pre>
          <pre style="margin: 0px">        fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaMap</span>&gt;);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> basket
= fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> pizza
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    sut.Add(pizza);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(p
=&gt; </pre>
          <pre style="margin: 0px">        p.Name == pizza.Name), <span style="color: #a31515">"Basket
has added pizza."</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this case the assertion examines the Pizze collection (you did know that the plural
of <em>pizza</em> is <em>pizze</em>, right?) of the <a href="http://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze.aspx">frozen
Basket</a> to verify that it contains the added pizza.
</p>
        <p>
The tricky part is that the Pizze property is a collection of Pizza instances, and
not PizzaPresenter instances. The injected IPizzaMap instance is responsible for mapping
from PizzaPresenter to Pizza, but since we are rewriting this as a state-based test,
I thought it would also be interesting to write the test without using <a href="http://code.google.com/p/moq/">Moq</a>.
Instead, we can use the real implementation of IPizzaMap, but this means that we must
instruct <a href="http://autofixture.codeplex.com/">AutoFixture</a> to map from the
abstract IPizzaMap to the concrete PizzaMap.
</p>
        <p>
We see that happening in this line of code:
</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;}??\fs20 fixture.Register&lt;\cf3 IPizzaMap\cf0 &gt;(\par ??    fixture.CreateAnonymous&lt;\cf3 PizzaMap\cf0 &gt;);}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;(</pre>
          <pre style="margin: 0px">    fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaMap</span>&gt;);</pre>
        </div>
        <p>
Notice the method group syntax: we pass in a delegate to the CreateAnonymous method,
which means that every time the fixture is asked to create an IPizzaMap instance,
it invokes CreateAnonymous&lt;PIzzaMap&gt;() and uses the result.
</p>
        <p>
This is, obviously, a general-purpose way in which we can map compatible types, so
we can write an extension method like this one:
</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 public\cf0  \cf1 static\cf0  \cf1 void\cf0  Register&lt;TAbstract, TConcrete&gt;(\par ??    \cf1 this\cf0  \cf4 Fixture\cf0  fixture) \cf1 where\cf0  TConcrete : TAbstract\par ??\{\par ??    fixture.Register&lt;TAbstract&gt;(() =&gt;\par ??        fixture.CreateAnonymous&lt;TConcrete&gt;());\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">static</span>
            <span style="color: blue">void</span> Register&lt;TAbstract,
TConcrete&gt;(</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span><span style="color: #2b91af">Fixture</span> fixture) <span style="color: blue">where</span> TConcrete
: TAbstract</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    fixture.Register&lt;TAbstract&gt;(() =&gt;</pre>
          <pre style="margin: 0px">        fixture.CreateAnonymous&lt;TConcrete&gt;());</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
(I’m slightly undecided on the name of this method. <em>Map</em> might be a better
name, but I just like the equivalence to some common DI Containers and their Register
methods.) Armed with this Register overload, we can now rewrite the previous Register
statement 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;}??\fs20 fixture.Register&lt;\cf3 IPizzaMap\cf0 , \cf3 PizzaMap\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>, <span style="color: #2b91af">PizzaMap</span>&gt;();</pre>
        </div>
        <p>
It’s the same amount of code lines, but I find it slightly more succinct and communicative.
</p>
        <p>
The real point of this blog post, however, is that you can map abstract types to concrete
types, and that you can always write extension methods to encapsulate your own AutoFixture
idioms.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a19c5021-1467-40fe-b0fd-c276fde0713e" />
      </body>
      <title>Mapping types with AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,a19c5021-1467-40fe-b0fd-c276fde0713e.aspx</guid>
      <link>http://blog.ploeh.dk/2010/04/06/MappingTypesWithAutoFixture.aspx</link>
      <pubDate>Tue, 06 Apr 2010 05:22:32 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx"&gt;previous&lt;/a&gt; &lt;a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx"&gt;posts&lt;/a&gt; I
demonstrated interaction-based unit tests that verify that a pizza is correctly being
added to a shopping basket. An alternative is a state-based test where we examine
the contents of the shopping basket after exercising the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
Here’s an initial attempt:
&lt;/p&gt;
&lt;!--
{\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  AddWillAddToBasket()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    fixture.Register&amp;lt;\cf3 IPizzaMap\cf0 &amp;gt;(\par ??        fixture.CreateAnonymous&amp;lt;\cf3 PizzaMap\cf0 &amp;gt;);\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&amp;lt;\cf3 Basket\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&amp;lt;\cf3 PizzaPresenter\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 BasketPresenter\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .IsTrue(basket.Pizze.Any(p =&amp;gt; \par ??        p.Name == pizza.Name), \cf6 "Basket has added pizza."\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddWillAddToBasket()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;&amp;gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaMap&lt;/span&gt;&amp;gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; basket
= fixture.Freeze&amp;lt;&lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; pizza
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;BasketPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Add(pizza);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(basket.Pizze.Any(p
=&amp;gt; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Name == pizza.Name), &lt;span style="color: #a31515"&gt;"Basket
has added pizza."&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this case the assertion examines the Pizze collection (you did know that the plural
of &lt;em&gt;pizza&lt;/em&gt; is &lt;em&gt;pizze&lt;/em&gt;, right?) of the &lt;a href="http://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze.aspx"&gt;frozen
Basket&lt;/a&gt; to verify that it contains the added pizza.
&lt;/p&gt;
&lt;p&gt;
The tricky part is that the Pizze property is a collection of Pizza instances, and
not PizzaPresenter instances. The injected IPizzaMap instance is responsible for mapping
from PizzaPresenter to Pizza, but since we are rewriting this as a state-based test,
I thought it would also be interesting to write the test without using &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt;.
Instead, we can use the real implementation of IPizzaMap, but this means that we must
instruct &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; to map from the
abstract IPizzaMap to the concrete PizzaMap.
&lt;/p&gt;
&lt;p&gt;
We see that happening in this line of code:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;}??\fs20 fixture.Register&amp;lt;\cf3 IPizzaMap\cf0 &amp;gt;(\par ??    fixture.CreateAnonymous&amp;lt;\cf3 PizzaMap\cf0 &amp;gt;);}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;&amp;gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaMap&lt;/span&gt;&amp;gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Notice the method group syntax: we pass in a delegate to the CreateAnonymous method,
which means that every time the fixture is asked to create an IPizzaMap instance,
it invokes CreateAnonymous&amp;lt;PIzzaMap&amp;gt;() and uses the result.
&lt;/p&gt;
&lt;p&gt;
This is, obviously, a general-purpose way in which we can map compatible types, so
we can write an extension method like this one:
&lt;/p&gt;
&lt;!--
{\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 public\cf0  \cf1 static\cf0  \cf1 void\cf0  Register&amp;lt;TAbstract, TConcrete&amp;gt;(\par ??    \cf1 this\cf0  \cf4 Fixture\cf0  fixture) \cf1 where\cf0  TConcrete : TAbstract\par ??\{\par ??    fixture.Register&amp;lt;TAbstract&amp;gt;(() =&amp;gt;\par ??        fixture.CreateAnonymous&amp;lt;TConcrete&amp;gt;());\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Register&amp;lt;TAbstract,
TConcrete&amp;gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt; fixture) &lt;span style="color: blue"&gt;where&lt;/span&gt; TConcrete
: TAbstract&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register&amp;lt;TAbstract&amp;gt;(() =&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;TConcrete&amp;gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
(I’m slightly undecided on the name of this method. &lt;em&gt;Map&lt;/em&gt; might be a better
name, but I just like the equivalence to some common DI Containers and their Register
methods.) Armed with this Register overload, we can now rewrite the previous Register
statement like this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;}??\fs20 fixture.Register&amp;lt;\cf3 IPizzaMap\cf0 , \cf3 PizzaMap\cf0 &amp;gt;();}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;PizzaMap&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
It’s the same amount of code lines, but I find it slightly more succinct and communicative.
&lt;/p&gt;
&lt;p&gt;
The real point of this blog post, however, is that you can map abstract types to concrete
types, and that you can always write extension methods to encapsulate your own AutoFixture
idioms.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a19c5021-1467-40fe-b0fd-c276fde0713e" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,a19c5021-1467-40fe-b0fd-c276fde0713e.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=9930c822-22bc-422d-a437-fb1e81e000a7</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,9930c822-22bc-422d-a437-fb1e81e000a7.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,9930c822-22bc-422d-a437-fb1e81e000a7.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=9930c822-22bc-422d-a437-fb1e81e000a7</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
My <a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx">previous post</a> about <a href="http://autofixture.codeplex.com/">AutoFixture</a>’s
Freeze functionality included this little piece of code that I didn’t discuss:
</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 var\cf0  mapMock = \cf1 new\cf0  \cf4 Mock\cf0 &lt;\cf4 IPizzaMap\cf0 &gt;();\par ??fixture.Register(mapMock.Object);}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mapMock
= <span style="color: blue">new</span><span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;();</pre>
          <pre style="margin: 0px">fixture.Register(mapMock.Object);</pre>
        </div>
        <p>
In case you were wondering, this is <a href="http://code.google.com/p/moq/">Moq</a> interacting
with AutoFixture. Here we create a new <a href="http://xunitpatterns.com/Test%20Double.html">Test
Double</a> and register it with the fixture. This is very similar to AutoFixture’s
built-in Freeze functionality, with the difference that we register an IPizzaMap instance,
which isn’t the same as the Mock&lt;IPizzaMap&gt; instance.
</p>
        <p>
It would be nice if we could simply freeze a Test Double emitted by Moq, but unfortunately
we can’t directly use the Freeze method, since Freeze&lt;Mock&lt;IPizzaMap&gt;&gt;()
would freeze a Mock&lt;IPizzaMap&gt;, but not IPizzaMap itself. On the other hand,
Freeze&lt;IPizzaMap&gt;() wouldn’t work because we haven’t told the fixture how to
create IPizzaMap instances, but even if we had, we wouldn’t have a Mock&lt;IPizzaMap&gt;
against which we could call Verify.
</p>
        <p>
On the other hand, it’s trivial to write an extension method to Fixture:
</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 public\cf0  \cf1 static\cf0  \cf4 Mock\cf0 &lt;T&gt; FreezeMoq&lt;T&gt;(\cf1 this\cf0  \cf4 Fixture\cf0  fixture)\par ??    \cf1 where\cf0  T : \cf1 class\par ??\cf0 \{\par ??    \cf1 var\cf0  td = \cf1 new\cf0  \cf4 Mock\cf0 &lt;T&gt;();\par ??    fixture.Register(td.Object);\par ??    \cf1 return\cf0  td;\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">static</span>
            <span style="color: #2b91af">Mock</span>&lt;T&gt;
FreezeMoq&lt;T&gt;(<span style="color: blue">this</span><span style="color: #2b91af">Fixture</span> fixture)</pre>
          <pre style="margin: 0px">    <span style="color: blue">where</span> T
: <span style="color: blue">class</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> td
= <span style="color: blue">new</span><span style="color: #2b91af">Mock</span>&lt;T&gt;();</pre>
          <pre style="margin: 0px">    fixture.Register(td.Object);</pre>
          <pre style="margin: 0px">    <span style="color: blue">return</span> td;</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
I chose to call the method FreezeMoq to indicate its affinity with Moq.
</p>
        <p>
We can now rewrite the unit test from the previous post 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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly_FreezeMoq()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&lt;\cf3 Basket\cf0 &gt;();\par ??    \cf4 var\cf0  mapMock = fixture.FreezeMoq&lt;\cf3 IPizzaMap\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&lt;\cf3 PizzaPresenter\cf0 &gt;();\par ??    \par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 BasketPresenter\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> AddWillPipeMapCorrectly_FreezeMoq()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> basket
= fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> mapMock
= fixture.FreezeMoq&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> pizza
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    sut.Add(pizza);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
You may think that saving only a single line of code may not be that big a deal, but
if you also need to perform Setups on the Mock, or if you have several different Mocks
to configure, you may appreciate the encapsulation. I know I sure do.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9930c822-22bc-422d-a437-fb1e81e000a7" />
      </body>
      <title>Freezing mocks</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,9930c822-22bc-422d-a437-fb1e81e000a7.aspx</guid>
      <link>http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx</link>
      <pubDate>Sat, 27 Mar 2010 13:27:02 GMT</pubDate>
      <description>&lt;p&gt;
My &lt;a href="http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx"&gt;previous post&lt;/a&gt; about &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s
Freeze functionality included this little piece of code that I didn’t discuss:
&lt;/p&gt;
&lt;!--
{\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 var\cf0  mapMock = \cf1 new\cf0  \cf4 Mock\cf0 &amp;lt;\cf4 IPizzaMap\cf0 &amp;gt;();\par ??fixture.Register(mapMock.Object);}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; mapMock
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.Register(mapMock.Object);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In case you were wondering, this is &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; interacting
with AutoFixture. Here we create a new &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test
Double&lt;/a&gt; and register it with the fixture. This is very similar to AutoFixture’s
built-in Freeze functionality, with the difference that we register an IPizzaMap instance,
which isn’t the same as the Mock&amp;lt;IPizzaMap&amp;gt; instance.
&lt;/p&gt;
&lt;p&gt;
It would be nice if we could simply freeze a Test Double emitted by Moq, but unfortunately
we can’t directly use the Freeze method, since Freeze&amp;lt;Mock&amp;lt;IPizzaMap&amp;gt;&amp;gt;()
would freeze a Mock&amp;lt;IPizzaMap&amp;gt;, but not IPizzaMap itself. On the other hand,
Freeze&amp;lt;IPizzaMap&amp;gt;() wouldn’t work because we haven’t told the fixture how to
create IPizzaMap instances, but even if we had, we wouldn’t have a Mock&amp;lt;IPizzaMap&amp;gt;
against which we could call Verify.
&lt;/p&gt;
&lt;p&gt;
On the other hand, it’s trivial to write an extension method to Fixture:
&lt;/p&gt;
&lt;!--
{\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 public\cf0  \cf1 static\cf0  \cf4 Mock\cf0 &amp;lt;T&amp;gt; FreezeMoq&amp;lt;T&amp;gt;(\cf1 this\cf0  \cf4 Fixture\cf0  fixture)\par ??    \cf1 where\cf0  T : \cf1 class\par ??\cf0 \{\par ??    \cf1 var\cf0  td = \cf1 new\cf0  \cf4 Mock\cf0 &amp;lt;T&amp;gt;();\par ??    fixture.Register(td.Object);\par ??    \cf1 return\cf0  td;\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;T&amp;gt;
FreezeMoq&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt; fixture)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; T
: &lt;span style="color: blue"&gt;class&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; td
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register(td.Object);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; td;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
I chose to call the method FreezeMoq to indicate its affinity with Moq.
&lt;/p&gt;
&lt;p&gt;
We can now rewrite the unit test from the previous post like this:
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly_FreezeMoq()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&amp;lt;\cf3 Basket\cf0 &amp;gt;();\par ??    \cf4 var\cf0  mapMock = fixture.FreezeMoq&amp;lt;\cf3 IPizzaMap\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&amp;lt;\cf3 PizzaPresenter\cf0 &amp;gt;();\par ??    \par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 BasketPresenter\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     mapMock.Verify(m =&amp;gt; m.Pipe(pizza, basket.Add));\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddWillPipeMapCorrectly_FreezeMoq()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; basket
= fixture.Freeze&amp;lt;&lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; mapMock
= fixture.FreezeMoq&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; pizza
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;BasketPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Add(pizza);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMock.Verify(m =&amp;gt; m.Pipe(pizza, basket.Add));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
You may think that saving only a single line of code may not be that big a deal, but
if you also need to perform Setups on the Mock, or if you have several different Mocks
to configure, you may appreciate the encapsulation. I know I sure do.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9930c822-22bc-422d-a437-fb1e81e000a7" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,9930c822-22bc-422d-a437-fb1e81e000a7.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=0b36f631-086d-4d79-a1ad-5153716987b0</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,0b36f631-086d-4d79-a1ad-5153716987b0.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,0b36f631-086d-4d79-a1ad-5153716987b0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=0b36f631-086d-4d79-a1ad-5153716987b0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze.aspx">previous blog
post</a>, I introduced <a href="http://autofixture.codeplex.com/">AutoFixture</a>’s
Freeze feature, but the example didn’t fully demonstrate the power of the concept.
In this blog post, we will turn up the heat on the frozen pizza a notch.
</p>
        <p>
The following unit test exercises the BasketPresenter class, which is simply a wrapper
around a Basket instance (we’re doing a pizza online shop, if you were wondering).
In true TDD style, I’ll start with the unit test, but I’ll post the BasketPresenter
class later for reference.
</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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&lt;\cf3 Basket\cf0 &gt;();\par ??\par ??    \cf4 var\cf0  mapMock = \cf4 new\cf0  \cf3 Mock\cf0 &lt;\cf3 IPizzaMap\cf0 &gt;();\par ??    fixture.Register(mapMock.Object);\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&lt;\cf3 PizzaPresenter\cf0 &gt;();\par ??    \par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 BasketPresenter\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> AddWillPipeMapCorrectly()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> basket
= fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> mapMock
= <span style="color: blue">new</span><span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;();</pre>
          <pre style="margin: 0px">    fixture.Register(mapMock.Object);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> pizza
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    sut.Add(pizza);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The interesting thing in the above unit test is that we Freeze a Basket instance in
the fixture. We do this because we know that the BasketPresenter <em>somehow</em> wraps
a Basket instance, but we trust the Fixture class to figure it out for us. By telling
the fixture instance to Freeze the Basket we know that it will reuse the same Basket
instance throughout the entire test case. That includes the call to CreateAnonymous&lt;BasketPresenter&gt;.
</p>
        <p>
This means that we can use the frozen basket instance in the Verify call because we
know that the same instance will have been reused by the fixture, and thus wrapped
by the <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
</p>
        <p>
When you stop to think about this on a more theoretical level, it fortunately makes
a lot of sense. AutoFixture’s terminology is based upon the excellent book <a href="http://xunitpatterns.com/">xUnit
Test Patterns</a>, and a Fixture instance pretty much corresponds to the concept of
a <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>.
This means that freezing an instance simply means that a particular instance is constant
throughout that particular fixture. Every time we ask for an instance of that class,
we get back the same frozen instance.
</p>
        <p>
In DI Container terminology, we just changed the Basket type’s lifetime behavior from
Transient to Singleton.
</p>
        <p>
For reference, here’s the BasketPresenter class we’re testing:
</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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 BasketPresenter\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Basket\cf0  basket;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 IPizzaMap\cf0  map;\par ??\par ??    \cf1 public\cf0  BasketPresenter(\cf4 Basket\cf0  basket, \cf4 IPizzaMap\cf0  map)\par ??    \{\par ??        \cf1 if\cf0  (basket == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "basket"\cf0 );\par ??        \}\par ??        \cf1 if\cf0  (map == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "map"\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .basket = basket;\par ??        \cf1 this\cf0 .map = map;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  Add(\cf4 PizzaPresenter\cf0  presenter)\par ??    \{\par ??        \cf1 this\cf0 .map.Pipe(presenter, \cf1 this\cf0 .basket.Add);\par ??    \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">BasketPresenter</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: #2b91af">Basket</span> basket;</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: #2b91af">IPizzaMap</span> map;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> BasketPresenter(<span style="color: #2b91af">Basket</span> basket, <span style="color: #2b91af">IPizzaMap</span> map)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (basket
== <span style="color: blue">null</span>)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">throw</span><span style="color: blue">new</span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"basket"</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (map
== <span style="color: blue">null</span>)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">throw</span><span style="color: blue">new</span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"map"</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.basket
= basket;</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.map
= map;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">void</span> Add(<span style="color: #2b91af">PizzaPresenter</span> presenter)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.map.Pipe(presenter, <span style="color: blue">this</span>.basket.Add);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
If you are wondering about why this is interesting at all, and why we don’t just pass
in a Basket through the BasketPresenter’s constructor, it’s because we are using AutoFixture
as a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT Factory</a>. We
want to be able to refactor BasketPresenter (and in this case particularly its constructor)
without breaking a lot of existing tests. The level of indirection provided by AutoFixture
gives us just that ability because we never directly invoke the constructor.
</p>
        <p>
Coming up: <a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx">more fun with
the Freeze concept</a>!
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0b36f631-086d-4d79-a1ad-5153716987b0" />
      </body>
      <title>More about frozen pizza</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,0b36f631-086d-4d79-a1ad-5153716987b0.aspx</guid>
      <link>http://blog.ploeh.dk/2010/03/26/MoreAboutFrozenPizza.aspx</link>
      <pubDate>Fri, 26 Mar 2010 22:01:42 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="http://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze.aspx"&gt;previous blog
post&lt;/a&gt;, I introduced &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s
Freeze feature, but the example didn’t fully demonstrate the power of the concept.
In this blog post, we will turn up the heat on the frozen pizza a notch.
&lt;/p&gt;
&lt;p&gt;
The following unit test exercises the BasketPresenter class, which is simply a wrapper
around a Basket instance (we’re doing a pizza online shop, if you were wondering).
In true TDD style, I’ll start with the unit test, but I’ll post the BasketPresenter
class later for reference.
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 var\cf0  basket = fixture.Freeze&amp;lt;\cf3 Basket\cf0 &amp;gt;();\par ??\par ??    \cf4 var\cf0  mapMock = \cf4 new\cf0  \cf3 Mock\cf0 &amp;lt;\cf3 IPizzaMap\cf0 &amp;gt;();\par ??    fixture.Register(mapMock.Object);\par ??\par ??    \cf4 var\cf0  pizza = fixture.CreateAnonymous&amp;lt;\cf3 PizzaPresenter\cf0 &amp;gt;();\par ??    \par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 BasketPresenter\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     sut.Add(pizza);\par ??    \cf5 // Verify outcome\par ??\cf0     mapMock.Verify(m =&amp;gt; m.Pipe(pizza, basket.Add));\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddWillPipeMapCorrectly()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; basket
= fixture.Freeze&amp;lt;&lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; mapMock
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register(mapMock.Object);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; pizza
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;PizzaPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;BasketPresenter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Add(pizza);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMock.Verify(m =&amp;gt; m.Pipe(pizza, basket.Add));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The interesting thing in the above unit test is that we Freeze a Basket instance in
the fixture. We do this because we know that the BasketPresenter &lt;em&gt;somehow&lt;/em&gt; wraps
a Basket instance, but we trust the Fixture class to figure it out for us. By telling
the fixture instance to Freeze the Basket we know that it will reuse the same Basket
instance throughout the entire test case. That includes the call to CreateAnonymous&amp;lt;BasketPresenter&amp;gt;.
&lt;/p&gt;
&lt;p&gt;
This means that we can use the frozen basket instance in the Verify call because we
know that the same instance will have been reused by the fixture, and thus wrapped
by the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
When you stop to think about this on a more theoretical level, it fortunately makes
a lot of sense. AutoFixture’s terminology is based upon the excellent book &lt;a href="http://xunitpatterns.com/"&gt;xUnit
Test Patterns&lt;/a&gt;, and a Fixture instance pretty much corresponds to the concept of
a &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;.
This means that freezing an instance simply means that a particular instance is constant
throughout that particular fixture. Every time we ask for an instance of that class,
we get back the same frozen instance.
&lt;/p&gt;
&lt;p&gt;
In DI Container terminology, we just changed the Basket type’s lifetime behavior from
Transient to Singleton.
&lt;/p&gt;
&lt;p&gt;
For reference, here’s the BasketPresenter class we’re testing:
&lt;/p&gt;
&lt;!--
{\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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 BasketPresenter\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Basket\cf0  basket;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 IPizzaMap\cf0  map;\par ??\par ??    \cf1 public\cf0  BasketPresenter(\cf4 Basket\cf0  basket, \cf4 IPizzaMap\cf0  map)\par ??    \{\par ??        \cf1 if\cf0  (basket == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "basket"\cf0 );\par ??        \}\par ??        \cf1 if\cf0  (map == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "map"\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .basket = basket;\par ??        \cf1 this\cf0 .map = map;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  Add(\cf4 PizzaPresenter\cf0  presenter)\par ??    \{\par ??        \cf1 this\cf0 .map.Pipe(presenter, \cf1 this\cf0 .basket.Add);\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BasketPresenter&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt; basket;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt; map;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; BasketPresenter(&lt;span style="color: #2b91af"&gt;Basket&lt;/span&gt; basket, &lt;span style="color: #2b91af"&gt;IPizzaMap&lt;/span&gt; map)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (basket
== &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"basket"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (map
== &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"map"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.basket
= basket;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.map
= map;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Add(&lt;span style="color: #2b91af"&gt;PizzaPresenter&lt;/span&gt; presenter)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.map.Pipe(presenter, &lt;span style="color: blue"&gt;this&lt;/span&gt;.basket.Add);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If you are wondering about why this is interesting at all, and why we don’t just pass
in a Basket through the BasketPresenter’s constructor, it’s because we are using AutoFixture
as a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT Factory&lt;/a&gt;. We
want to be able to refactor BasketPresenter (and in this case particularly its constructor)
without breaking a lot of existing tests. The level of indirection provided by AutoFixture
gives us just that ability because we never directly invoke the constructor.
&lt;/p&gt;
&lt;p&gt;
Coming up: &lt;a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx"&gt;more fun with
the Freeze concept&lt;/a&gt;!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0b36f631-086d-4d79-a1ad-5153716987b0" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,0b36f631-086d-4d79-a1ad-5153716987b0.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=23c49b8a-f9e3-4012-8172-d7cf4584341d</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,23c49b8a-f9e3-4012-8172-d7cf4584341d.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,23c49b8a-f9e3-4012-8172-d7cf4584341d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=23c49b8a-f9e3-4012-8172-d7cf4584341d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of the important points of <a href="http://autofixture.codeplex.com/">AutoFixture</a> is
to hide away all the boring details that you don’t care about when you are writing
a unit test, but that the compiler seems to insist upon. One of these details is how
you create a new instance of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
</p>
        <p>
Every time you create an instance of your SUT using its constructor, you make it more
difficult to refactor that constructor. This is particularly true when it comes to
Constructor Injection because you often need to define a <a href="http://xunitpatterns.com/Test%20Double.html">Test
Double</a> in each unit test, but even for primitive types, it’s more maintenance-friendly
to use a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT Factory</a>.
</p>
        <p>
AutoFixture is a SUT Factory, so we can use it to create instances of our SUTs. However,
how do we correlate constructor parameters with variables in the test when we will
not use the constructor directly?
</p>
        <p>
This is where the Freeze method comes in handy, but let’s first examine how to do
it with the core API methods <a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx">CreateAnonymous</a> and <a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx">Register</a>.
</p>
        <p>
Imagine that we want to write a unit test for a Pizza class that takes a name in its
constructor and exposes that name as a property. We can write this test 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  NameIsCorrect()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 var\cf0  expectedName = fixture.CreateAnonymous(\cf6 "Name"\cf0 );\par ??    fixture.Register(expectedName);\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 Pizza\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Name;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual(expectedName, result, \cf6 "Name"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> NameIsCorrect()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> expectedName
= fixture.CreateAnonymous(<span style="color: #a31515">"Name"</span>);</pre>
          <pre style="margin: 0px">    fixture.Register(expectedName);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Pizza</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Name;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual(expectedName,
result, <span style="color: #a31515">"Name"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The important lines are these two:
</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;\red163\green21\blue21;}??\fs20 \cf1 var\cf0  expectedName = fixture.CreateAnonymous(\cf4 "Name"\cf0 );\par ??fixture.Register(expectedName);}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> expectedName
= fixture.CreateAnonymous(<span style="color: #a31515">"Name"</span>);</pre>
          <pre style="margin: 0px">fixture.Register(expectedName);</pre>
        </div>
        <p>
What’s going on here is that we create a new string, and then we subsequently Register
this string so that <em>every time</em> the fixture instance is asked to create a
string, it will return this particular string. This also means that when we ask AutoFixture
to create an instance of Pizza, it will use that string as the constructor parameter.
</p>
        <p>
It turned out that we used this coding idiom so much that we decided to encapsulate
it in a convenience method. After some debate we arrived at the name Freeze, because
we essentially freeze a single anonymous variable in the fixture, bypassing the default
algorithm for creating new instances. Incidentally, this is one of very few methods
in AutoFixture that breaks CQS, but although that bugs me a little, the Freeze concept
has turned out to be so powerful that I live with it.
</p>
        <p>
Here is the same test rewritten to use the Freeze method:
</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  NameIsCorrect_Freeze()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf4 var\cf0  expectedName = fixture.Freeze(\cf6 "Name"\cf0 );\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 Pizza\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Name;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual(expectedName, result, \cf6 "Name"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> NameIsCorrect_Freeze()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> expectedName
= fixture.Freeze(<span style="color: #a31515">"Name"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Pizza</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Name;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual(expectedName,
result, <span style="color: #a31515">"Name"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this example, we only save a single line of code, but apart from that, the test
also becomes a little more communicative because it explicitly calls out that this
particular string is frozen.
</p>
        <p>
However, this is still a pretty lame example, but while I intend to follow up with
a more complex example, I wanted to introduce the concept gently.
</p>
        <p>
For completeness sake, here’s the Pizza class:
</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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 Pizza\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 string\cf0  name;\par ??\par ??    \cf1 public\cf0  Pizza(\cf1 string\cf0  name)\par ??    \{\par ??        \cf1 if\cf0  (name == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "name"\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .name = name;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Name\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .name; \}\par ??    \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">Pizza</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: blue">string</span> name;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> Pizza(<span style="color: blue">string</span> name)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (name
== <span style="color: blue">null</span>)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">throw</span><span style="color: blue">new</span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"name"</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.name
= name;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">string</span> Name</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">get</span> { <span style="color: blue">return</span><span style="color: blue">this</span>.name;
}</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
As you can see, the test simply verifies that the constructor parameter is echoed
by the Name property, and the Freeze method makes this more explicit while we still
enjoy the indirection of not invoking the constructor directly.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=23c49b8a-f9e3-4012-8172-d7cf4584341d" />
      </body>
      <title>AutoFixture Freeze</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,23c49b8a-f9e3-4012-8172-d7cf4584341d.aspx</guid>
      <link>http://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze.aspx</link>
      <pubDate>Wed, 17 Mar 2010 21:54:53 GMT</pubDate>
      <description>&lt;p&gt;
One of the important points of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; is
to hide away all the boring details that you don’t care about when you are writing
a unit test, but that the compiler seems to insist upon. One of these details is how
you create a new instance of your &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Every time you create an instance of your SUT using its constructor, you make it more
difficult to refactor that constructor. This is particularly true when it comes to
Constructor Injection because you often need to define a &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test
Double&lt;/a&gt; in each unit test, but even for primitive types, it’s more maintenance-friendly
to use a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT Factory&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
AutoFixture is a SUT Factory, so we can use it to create instances of our SUTs. However,
how do we correlate constructor parameters with variables in the test when we will
not use the constructor directly?
&lt;/p&gt;
&lt;p&gt;
This is where the Freeze method comes in handy, but let’s first examine how to do
it with the core API methods &lt;a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx"&gt;CreateAnonymous&lt;/a&gt; and &lt;a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx"&gt;Register&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Imagine that we want to write a unit test for a Pizza class that takes a name in its
constructor and exposes that name as a property. We can write this test like this:
&lt;/p&gt;
&lt;!--
{\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  NameIsCorrect()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 var\cf0  expectedName = fixture.CreateAnonymous(\cf6 "Name"\cf0 );\par ??    fixture.Register(expectedName);\par ??\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 Pizza\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Name;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual(expectedName, result, \cf6 "Name"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NameIsCorrect()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; expectedName
= fixture.CreateAnonymous(&lt;span style="color: #a31515"&gt;"Name"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register(expectedName);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Pizza&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Name;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(expectedName,
result, &lt;span style="color: #a31515"&gt;"Name"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The important lines are these two:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \cf1 var\cf0  expectedName = fixture.CreateAnonymous(\cf4 "Name"\cf0 );\par ??fixture.Register(expectedName);}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; expectedName
= fixture.CreateAnonymous(&lt;span style="color: #a31515"&gt;"Name"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.Register(expectedName);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
What’s going on here is that we create a new string, and then we subsequently Register
this string so that &lt;em&gt;every time&lt;/em&gt; the fixture instance is asked to create a
string, it will return this particular string. This also means that when we ask AutoFixture
to create an instance of Pizza, it will use that string as the constructor parameter.
&lt;/p&gt;
&lt;p&gt;
It turned out that we used this coding idiom so much that we decided to encapsulate
it in a convenience method. After some debate we arrived at the name Freeze, because
we essentially freeze a single anonymous variable in the fixture, bypassing the default
algorithm for creating new instances. Incidentally, this is one of very few methods
in AutoFixture that breaks CQS, but although that bugs me a little, the Freeze concept
has turned out to be so powerful that I live with it.
&lt;/p&gt;
&lt;p&gt;
Here is the same test rewritten to use the Freeze method:
&lt;/p&gt;
&lt;!--
{\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  NameIsCorrect_Freeze()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf4 var\cf0  expectedName = fixture.Freeze(\cf6 "Name"\cf0 );\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 Pizza\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Name;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual(expectedName, result, \cf6 "Name"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NameIsCorrect_Freeze()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; expectedName
= fixture.Freeze(&lt;span style="color: #a31515"&gt;"Name"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Pizza&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Name;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(expectedName,
result, &lt;span style="color: #a31515"&gt;"Name"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this example, we only save a single line of code, but apart from that, the test
also becomes a little more communicative because it explicitly calls out that this
particular string is frozen.
&lt;/p&gt;
&lt;p&gt;
However, this is still a pretty lame example, but while I intend to follow up with
a more complex example, I wanted to introduce the concept gently.
&lt;/p&gt;
&lt;p&gt;
For completeness sake, here’s the Pizza class:
&lt;/p&gt;
&lt;!--
{\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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 Pizza\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 string\cf0  name;\par ??\par ??    \cf1 public\cf0  Pizza(\cf1 string\cf0  name)\par ??    \{\par ??        \cf1 if\cf0  (name == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "name"\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .name = name;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Name\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .name; \}\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Pizza&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; name;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; Pizza(&lt;span style="color: blue"&gt;string&lt;/span&gt; name)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (name
== &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"name"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.name
= name;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Name&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.name;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
As you can see, the test simply verifies that the constructor parameter is echoed
by the Name property, and the Freeze method makes this more explicit while we still
enjoy the indirection of not invoking the constructor directly.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=23c49b8a-f9e3-4012-8172-d7cf4584341d" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,23c49b8a-f9e3-4012-8172-d7cf4584341d.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=b4ca21d4-9046-4cff-bb74-3d0a50c958de</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,b4ca21d4-9046-4cff-bb74-3d0a50c958de.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,b4ca21d4-9046-4cff-bb74-3d0a50c958de.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=b4ca21d4-9046-4cff-bb74-3d0a50c958de</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In a <a href="http://blog.ploeh.dk/2009/11/26/AnonymousDo.aspx">previous post</a> I
described how <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s Do method
can let you invoke commands on your <a href="http://xunitpatterns.com/SUT.html">SUT</a> with <a href="http://xunitpatterns.com/Dummy%20Object.html">Dummy</a> values
for the parameters you don't care about.
</p>
        <p>
The Get method is the equivalent method you can use when the member you are invoking
returns a value. In other words: if you want to call a method on your SUT to get a
value, but you don't want the hassle of coming up with values you don't care about,
you can let the Get method supply those values for you.
</p>
        <p>
In today's example I will demonstrate a unit test that verifies the behavior of a
custom ASP.NET MVC ModelBinder. If you don't know anything about ASP.NET MVC it doesn't
really matter. The point is that a ModelBinder must implement the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imodelbinder.aspx">IModelBinder</a> interface
that defines a single method:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">object</span> BindModel(<span style="color: #2b91af">ControllerContext</span> controllerContext,
</p>
          <p style="margin: 0px">
    <span style="color: #2b91af">ModelBindingContext</span> bindingContext);
</p>
        </div>
        <p>
In many cases we don't care about one or the other of these parameters, but we still
need to supply them when unit testing.
</p>
        <p>
The example is a bit more complex than some of my other sample code, but once in a
while I like to provide you with slightly more realistic AutoFixture examples. Still,
it's only 10 lines of code, but it looks like a lot more because I have wrapped several
of the lines so that the code is still readable on small screens.
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
[<span style="color: #2b91af">TestMethod</span>]
</p>
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> BindModelWillReturnCorrectResult()
</p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
    <span style="color: green">// Fixture setup</span></p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> fixture = <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();
</p>
          <p style="margin: 0px">
    fixture.Customize&lt;<span style="color: #2b91af">ControllerContext</span>&gt;(ob
=&gt;
</p>
          <p style="margin: 0px">
        ob.OmitAutoProperties());
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> value = fixture.CreateAnonymous(<span style="color: #a31515">"Value"</span>);
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> bindingContext = <span style="color: blue">new</span><span style="color: #2b91af">ModelBindingContext</span>();
</p>
          <p style="margin: 0px">
    bindingContext.ValueProvider = 
</p>
          <p style="margin: 0px">
        <span style="color: blue">new</span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">ValueProviderResult</span>&gt;();
</p>
          <p style="margin: 0px">
    bindingContext.ValueProvider[<span style="color: #a31515">"MyValue"</span>]
= 
</p>
          <p style="margin: 0px">
        <span style="color: blue">new</span><span style="color: #2b91af">ValueProviderResult</span>(value,
value, 
</p>
          <p style="margin: 0px">
            <span style="color: #2b91af">CultureInfo</span>.CurrentCulture);
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> expectedResult = 
</p>
          <p style="margin: 0px">
        <span style="color: blue">new</span><span style="color: blue">string</span>(value.Reverse().ToArray());
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyModelBinder</span>&gt;();
</p>
          <p style="margin: 0px">
    <span style="color: green">// Exercise system</span></p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> result = fixture.Get((<span style="color: #2b91af">ControllerContext</span> cc)
=&gt;
</p>
          <p style="margin: 0px">
        sut.BindModel(cc, bindingContext));
</p>
          <p style="margin: 0px">
    <span style="color: green">// Verify outcome</span></p>
          <p style="margin: 0px">
    <span style="color: #2b91af">Assert</span>.AreEqual(expectedResult,
result, <span style="color: #a31515">"BindModel"</span>);
</p>
          <p style="margin: 0px">
    <span style="color: green">// Teardown</span></p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
The first part simply creates the Fixture object and <a href="http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx">customizes</a> it
to <a href="http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx">disable
AutoProperties</a> for all ControllerContext instances (otherwise we would have to
set up a lot of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> for
such properties as HttpContext, RequestContext etc.).
</p>
        <p>
The next part of the test sets up a ModelBindingContext instance that will be used
to exercise the SUT. In this test case, the <em>bindingContext</em> parameter of the
BindModel method is important, so I explicitly set that up. On the other hand, I don't
care about the <em>controllerContext</em> parameter in this test case, so I ask the
Get method to take care of that for me:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">var</span> result = fixture.Get((<span style="color: #2b91af">ControllerContext</span> cc)
=&gt;
</p>
          <p style="margin: 0px">
    sut.BindModel(cc, bindingContext));
</p>
        </div>
        <p>
The Get method creates a Dummy value for the ControllerContext, whereas I can still
use the outer variable <em>bindingContext</em> to call the BindModel method. The return
value of the BindModel method is returned to the <em>result</em> variable by the Get
method.
</p>
        <p>
Like the Do methods, the Get methods are generic methods. The one invoked in this
example has this signature:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">public</span> TResult Get&lt;T, TResult&gt;(<span style="color: #2b91af">Func</span>&lt;T,
TResult&gt; function);
</p>
        </div>
        <p>
There are also overloads that works with the versions of the Func delegate that takes
two, three and four parameters.
</p>
        <p>
As the Do methods, the Get methods are convenience methods that let you concentrate
on writing the test code you care about while it takes care of all the rest. You could
have written a slightly more complex version that didn't use Get but instead used
the CreateAnonymous method to create an Anonymous Variable for the ControllerContext,
but this way is slightly more succinct.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b4ca21d4-9046-4cff-bb74-3d0a50c958de" />
      </body>
      <title>Anonymous Get</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,b4ca21d4-9046-4cff-bb74-3d0a50c958de.aspx</guid>
      <link>http://blog.ploeh.dk/2010/01/04/AnonymousGet.aspx</link>
      <pubDate>Mon, 04 Jan 2010 20:53:24 GMT</pubDate>
      <description>&lt;p&gt;
In a &lt;a href="http://blog.ploeh.dk/2009/11/26/AnonymousDo.aspx"&gt;previous post&lt;/a&gt; I
described how &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;'s Do method
can let you invoke commands on your &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; with &lt;a href="http://xunitpatterns.com/Dummy%20Object.html"&gt;Dummy&lt;/a&gt; values
for the parameters you don't care about.
&lt;/p&gt;
&lt;p&gt;
The Get method is the equivalent method you can use when the member you are invoking
returns a value. In other words: if you want to call a method on your SUT to get a
value, but you don't want the hassle of coming up with values you don't care about,
you can let the Get method supply those values for you.
&lt;/p&gt;
&lt;p&gt;
In today's example I will demonstrate a unit test that verifies the behavior of a
custom ASP.NET MVC ModelBinder. If you don't know anything about ASP.NET MVC it doesn't
really matter. The point is that a ModelBinder must implement the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imodelbinder.aspx"&gt;IModelBinder&lt;/a&gt; interface
that defines a single method:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;object&lt;/span&gt; BindModel(&lt;span style="color: #2b91af"&gt;ControllerContext&lt;/span&gt; controllerContext,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ModelBindingContext&lt;/span&gt; bindingContext);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
In many cases we don't care about one or the other of these parameters, but we still
need to supply them when unit testing.
&lt;/p&gt;
&lt;p&gt;
The example is a bit more complex than some of my other sample code, but once in a
while I like to provide you with slightly more realistic AutoFixture examples. Still,
it's only 10 lines of code, but it looks like a lot more because I have wrapped several
of the lines so that the code is still readable on small screens.
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; BindModelWillReturnCorrectResult()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Customize&amp;lt;&lt;span style="color: #2b91af"&gt;ControllerContext&lt;/span&gt;&amp;gt;(ob
=&amp;gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ob.OmitAutoProperties());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; value = fixture.CreateAnonymous(&lt;span style="color: #a31515"&gt;"Value"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; bindingContext = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ModelBindingContext&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; bindingContext.ValueProvider = 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ValueProviderResult&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; bindingContext.ValueProvider[&lt;span style="color: #a31515"&gt;"MyValue"&lt;/span&gt;]
= 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ValueProviderResult&lt;/span&gt;(value,
value, 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;CultureInfo&lt;/span&gt;.CurrentCulture);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; expectedResult = 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(value.Reverse().ToArray());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut = fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyModelBinder&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; result = fixture.Get((&lt;span style="color: #2b91af"&gt;ControllerContext&lt;/span&gt; cc)
=&amp;gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.BindModel(cc, bindingContext));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(expectedResult,
result, &lt;span style="color: #a31515"&gt;"BindModel"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The first part simply creates the Fixture object and &lt;a href="http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx"&gt;customizes&lt;/a&gt; it
to &lt;a href="http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx"&gt;disable
AutoProperties&lt;/a&gt; for all ControllerContext instances (otherwise we would have to
set up a lot of &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Doubles&lt;/a&gt; for
such properties as HttpContext, RequestContext etc.).
&lt;/p&gt;
&lt;p&gt;
The next part of the test sets up a ModelBindingContext instance that will be used
to exercise the SUT. In this test case, the &lt;em&gt;bindingContext&lt;/em&gt; parameter of the
BindModel method is important, so I explicitly set that up. On the other hand, I don't
care about the &lt;em&gt;controllerContext&lt;/em&gt; parameter in this test case, so I ask the
Get method to take care of that for me:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;var&lt;/span&gt; result = fixture.Get((&lt;span style="color: #2b91af"&gt;ControllerContext&lt;/span&gt; cc)
=&amp;gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.BindModel(cc, bindingContext));
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The Get method creates a Dummy value for the ControllerContext, whereas I can still
use the outer variable &lt;em&gt;bindingContext&lt;/em&gt; to call the BindModel method. The return
value of the BindModel method is returned to the &lt;em&gt;result&lt;/em&gt; variable by the Get
method.
&lt;/p&gt;
&lt;p&gt;
Like the Do methods, the Get methods are generic methods. The one invoked in this
example has this signature:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; TResult Get&amp;lt;T, TResult&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,
TResult&amp;gt; function);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
There are also overloads that works with the versions of the Func delegate that takes
two, three and four parameters.
&lt;/p&gt;
&lt;p&gt;
As the Do methods, the Get methods are convenience methods that let you concentrate
on writing the test code you care about while it takes care of all the rest. You could
have written a slightly more complex version that didn't use Get but instead used
the CreateAnonymous method to create an Anonymous Variable for the ControllerContext,
but this way is slightly more succinct.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b4ca21d4-9046-4cff-bb74-3d0a50c958de" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,b4ca21d4-9046-4cff-bb74-3d0a50c958de.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=9da2b80c-55e5-41e9-9940-f660e561204a</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=9da2b80c-55e5-41e9-9940-f660e561204a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In unit testing an important step is to exercise the <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
The member you want to invoke is often a method that takes one or more parameters,
but in some test cases you don't care about the values of those parameters – you just
want to invoke the method.
</p>
        <p>
You can always make up one or more <a href="http://xunitpatterns.com/Dummy%20Object.html">Dummy</a> parameters
and pass them to the method in question, but you could also use one of <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s
Do convenience methods. There are several overloads that all take delegates that specify
the action in question while providing you with Dummies of any parameters you don't
care about.
</p>
        <p>
A good example is WPF's <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx">ICommand</a> interface.
The most prevalent method is the Execute method that takes a single <em>parameter</em> parameter:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">void</span> Execute(<span style="color: blue">object</span> parameter);
</p>
        </div>
        <p>
Most of the time we don't really care about the parameter because we only care that
the command was invoked. However, we still need to supply a value for the parameter
when we unit test our ICommand implementations. Obviously, we could just pass in a
new System.Object every time, but why not let AutoFixture take care of that for us?
</p>
        <p>
(You may think that new'ing up a System.Object is something you can easily do yourself,
but imagine other APIs that require much more complex input parameters, and you should
begin to see the potential.)
</p>
        <p>
Here's a state-based unit test that verifies that the Message property of the MyViewModel
class has the correct value after the FooCommand has been invoked:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
[<span style="color: #2b91af">TestMethod</span>]
</p>
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> FooWillUpdateMessage()
</p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
    <span style="color: green">// Fixture setup</span></p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> fixture = <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;();
</p>
          <p style="margin: 0px">
    <span style="color: green">// Exercise system</span></p>
          <p style="margin: 0px">
    fixture.Do((<span style="color: blue">object</span> parameter)
=&gt; 
</p>
          <p style="margin: 0px">
        sut.FooCommand.Execute(parameter));
</p>
          <p style="margin: 0px">
    <span style="color: green">// Verify outcome</span></p>
          <p style="margin: 0px">
    <span style="color: #2b91af">Assert</span>.AreEqual(<span style="color: #a31515">"Foo"</span>,
sut.Message, <span style="color: #a31515">"Message"</span>);
</p>
          <p style="margin: 0px">
    <span style="color: green">// Teardown</span></p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
Notice how the Do method takes an Action&lt;object&gt; to specify the method to invoke.
AutoFixture automatically supplies an instance for the <em>parameter</em> parameter
using the same engine to create <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
variables</a> that it uses for everything else.
</p>
        <p>
The Do method in question is really a generic method with this signature:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> Do&lt;T&gt;(<span style="color: #2b91af">Action</span>&lt;T&gt;
action);
</p>
        </div>
        <p>
There are also overloads that take two, three or four input parameters, corresponding
to the available Action types available in the BCL.
</p>
        <p>
These methods are simply convenience methods that allow you to express your test code
more succinctly than you would otherwise have been able to do.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9da2b80c-55e5-41e9-9940-f660e561204a" />
      </body>
      <title>Anonymous Do</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</guid>
      <link>http://blog.ploeh.dk/2009/11/26/AnonymousDo.aspx</link>
      <pubDate>Thu, 26 Nov 2009 21:23:46 GMT</pubDate>
      <description>&lt;p&gt;
In unit testing an important step is to exercise the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
The member you want to invoke is often a method that takes one or more parameters,
but in some test cases you don't care about the values of those parameters – you just
want to invoke the method.
&lt;/p&gt;
&lt;p&gt;
You can always make up one or more &lt;a href="http://xunitpatterns.com/Dummy%20Object.html"&gt;Dummy&lt;/a&gt; parameters
and pass them to the method in question, but you could also use one of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;'s
Do convenience methods. There are several overloads that all take delegates that specify
the action in question while providing you with Dummies of any parameters you don't
care about.
&lt;/p&gt;
&lt;p&gt;
A good example is WPF's &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx"&gt;ICommand&lt;/a&gt; interface.
The most prevalent method is the Execute method that takes a single &lt;em&gt;parameter&lt;/em&gt; parameter:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: blue"&gt;object&lt;/span&gt; parameter);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Most of the time we don't really care about the parameter because we only care that
the command was invoked. However, we still need to supply a value for the parameter
when we unit test our ICommand implementations. Obviously, we could just pass in a
new System.Object every time, but why not let AutoFixture take care of that for us?
&lt;/p&gt;
&lt;p&gt;
(You may think that new'ing up a System.Object is something you can easily do yourself,
but imagine other APIs that require much more complex input parameters, and you should
begin to see the potential.)
&lt;/p&gt;
&lt;p&gt;
Here's a state-based unit test that verifies that the Message property of the MyViewModel
class has the correct value after the FooCommand has been invoked:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; FooWillUpdateMessage()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut = fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Do((&lt;span style="color: blue"&gt;object&lt;/span&gt; parameter)
=&amp;gt; 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.FooCommand.Execute(parameter));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: #a31515"&gt;"Foo"&lt;/span&gt;,
sut.Message, &lt;span style="color: #a31515"&gt;"Message"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Notice how the Do method takes an Action&amp;lt;object&amp;gt; to specify the method to invoke.
AutoFixture automatically supplies an instance for the &lt;em&gt;parameter&lt;/em&gt; parameter
using the same engine to create &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
variables&lt;/a&gt; that it uses for everything else.
&lt;/p&gt;
&lt;p&gt;
The Do method in question is really a generic method with this signature:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Do&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;
action);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
There are also overloads that take two, three or four input parameters, corresponding
to the available Action types available in the BCL.
&lt;/p&gt;
&lt;p&gt;
These methods are simply convenience methods that allow you to express your test code
more succinctly than you would otherwise have been able to do.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9da2b80c-55e5-41e9-9940-f660e561204a" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
      <category>WPF</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=43e6e4e3-6e34-4100-9106-3c9fa12b9e8c</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,43e6e4e3-6e34-4100-9106-3c9fa12b9e8c.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,43e6e4e3-6e34-4100-9106-3c9fa12b9e8c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=43e6e4e3-6e34-4100-9106-3c9fa12b9e8c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A few months ago I described how you can use <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s <a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx">With
method</a> to assign property values as part of building up an anonymous variable.
In that scenario, the With method is used to explicitly assign a particular, pre-defined
value to a property.
</p>
        <p>
There's another overload of the With method that <em>doesn't</em> take an explicit
value, but rather uses AutoFixture to create an anonymous value for the property.
So what's the deal with that if AutoFixture's default behavior is to assign anonymous
values to all writable properties?
</p>
        <p>
In short it's an opt-in mechanism that only makes sense if you decide to opt out of
the AutoProperties features.
</p>
        <p>
As always, let's look at an example. This time, I've decided to show you a slightly
more realistic example so that you can get an idea of how AutoFixture can be used
to aid in unit testing. This also means that the example is going to be a little more
complex than usual, but it's still simple.
</p>
        <p>
Imagine that we wish to test that an ASP.NET MVC Controller Action returns the correct
result. More specifically, we wish to test that the Profile method on MyController
returns a ViewResult with the correct Model (the current user's name, to make things
interesing).
</p>
        <p>
Here's the entire test. It may look more complicated than it is – it's really only
10 lines of code, but I had to break them up to prevent unpleasant wrapping if your
screen is narrow.
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <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> ProfileWillReturnResultWithCorrectUserName()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    fixture.Customize&lt;<span style="color: #2b91af">ControllerContext</span>&gt;(ob
=&gt; ob</pre>
          <pre style="margin: 0px">        .OmitAutoProperties()</pre>
          <pre style="margin: 0px">        .With(cc =&gt; cc.HttpContext));</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> expectedUserName
= </pre>
          <pre style="margin: 0px">        fixture.CreateAnonymous(<span style="color: #a31515">"UserName"</span>);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> httpCtxStub
= <span style="color: blue">new</span><span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt;();</pre>
          <pre style="margin: 0px">    httpCtxStub.SetupGet(x =&gt; x.User).Returns(</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: #2b91af">GenericPrincipal</span>(</pre>
          <pre style="margin: 0px">            <span style="color: blue">new</span><span style="color: #2b91af">GenericIdentity</span>(expectedUserName), <span style="color: blue">null</span>));</pre>
          <pre style="margin: 0px">    fixture.Register(httpCtxStub.Object);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.Build&lt;<span style="color: #2b91af">MyController</span>&gt;()</pre>
          <pre style="margin: 0px">        .OmitAutoProperties()</pre>
          <pre style="margin: 0px">        .With(c =&gt; c.ControllerContext)</pre>
          <pre style="margin: 0px">        .CreateAnonymous();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">ViewResult</span> result
= sut.Profile();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> actual
= result.ViewData.Model;</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual(expectedUserName,
actual, <span style="color: #a31515">"User"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Apart from AutoFixture, I'm also making use of <a href="http://code.google.com/p/moq/">Moq</a> to
stub out HttpContextBase.
</p>
        <p>
You can see the Anonymous With method in two different places: in the call to <a href="http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx">Customize</a> and
when the <a href="http://xunitpatterns.com/SUT.html">SUT</a> is being built. In both
cases you can see that the call to With follows a call to OmitAutoProperties. In other
words: we are telling AutoFixture that we don't want any of the writable properties
to be assigned a value <em>except</em> the one we identify.
</p>
        <p>
Let me highlight some parts of the test.
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">fixture.Customize&lt;<span style="color: #2b91af">ControllerContext</span>&gt;(ob
=&gt; ob</pre>
          <pre style="margin: 0px">    .OmitAutoProperties()</pre>
          <pre style="margin: 0px">    .With(cc =&gt; cc.HttpContext));</pre>
        </div>
        <p>
This line of code instructs AutoFixture to always create a ControllerContext in a
particular way: I don't want to use AutoProperties here, because ControllerContext
has a lot of writable properties of abstract types, and that would require me to set
up a lot of Test Doubles if I had to assign values to all of those. It's much easier
to simply opt out of this mechanism. However, I <em>would</em> like to have the HttpContext
property assigned, but I don't care about the value in this statement, so the With
method simply states that AutoFixture should assign a value according to whatever
rule it has for creating instances of HttpContextBase.
</p>
        <p>
I can now set up a Stub that populates the User property of HttpContextBase:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> httpCtxStub
= <span style="color: blue">new</span><span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt;();</pre>
          <pre style="margin: 0px">httpCtxStub.SetupGet(x =&gt; x.User).Returns(</pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">GenericPrincipal</span>(</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: #2b91af">GenericIdentity</span>(expectedUserName), <span style="color: blue">null</span>));</pre>
          <pre style="margin: 0px">fixture.Register(httpCtxStub.Object);</pre>
        </div>
        <p>
This is registered with the fixture instance which closes the loop to the previous
customization.
</p>
        <p>
I can now create an instance of my SUT. Once more, I don't want to have to set up
a lot of irrelevant properties on MyController, so I opt out of AutoProperties and
then explicitly opt in on the ControllerContext. This will cause AutoFixture to automatically
populate the ControllerContext with the HttpContext Stub:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> sut
= fixture.Build&lt;<span style="color: #2b91af">MyController</span>&gt;()</pre>
          <pre style="margin: 0px">    .OmitAutoProperties()</pre>
          <pre style="margin: 0px">    .With(c =&gt; c.ControllerContext)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
For completeness' sake, here's the MyController class that I am testing:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">MyController</span> : <span style="color: #2b91af">Controller</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">ViewResult</span> Profile()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">object</span> userName
= <span style="color: blue">this</span>.User.Identity.Name;</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span><span style="color: blue">this</span>.View(userName);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
This test may seem complex, but it really accomplishes a lot in only 10 lines of code,
considering that ASP.NET MVC Controllers with a working HttpContext are not particularly
trivial to set up.
</p>
        <p>
In summary, this With method overload lets you opt in on one or more explicitly identified
properties when you have otherwise decided to omit AutoProperties. As all the other
Builder methods, this method can also be chained.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=43e6e4e3-6e34-4100-9106-3c9fa12b9e8c" />
      </body>
      <title>Anonymous With</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,43e6e4e3-6e34-4100-9106-3c9fa12b9e8c.aspx</guid>
      <link>http://blog.ploeh.dk/2009/10/26/AnonymousWith.aspx</link>
      <pubDate>Mon, 26 Oct 2009 20:26:49 GMT</pubDate>
      <description>&lt;p&gt;
A few months ago I described how you can use &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;'s &lt;a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx"&gt;With
method&lt;/a&gt; to assign property values as part of building up an anonymous variable.
In that scenario, the With method is used to explicitly assign a particular, pre-defined
value to a property.
&lt;/p&gt;
&lt;p&gt;
There's another overload of the With method that &lt;em&gt;doesn't&lt;/em&gt; take an explicit
value, but rather uses AutoFixture to create an anonymous value for the property.
So what's the deal with that if AutoFixture's default behavior is to assign anonymous
values to all writable properties?
&lt;/p&gt;
&lt;p&gt;
In short it's an opt-in mechanism that only makes sense if you decide to opt out of
the AutoProperties features.
&lt;/p&gt;
&lt;p&gt;
As always, let's look at an example. This time, I've decided to show you a slightly
more realistic example so that you can get an idea of how AutoFixture can be used
to aid in unit testing. This also means that the example is going to be a little more
complex than usual, but it's still simple.
&lt;/p&gt;
&lt;p&gt;
Imagine that we wish to test that an ASP.NET MVC Controller Action returns the correct
result. More specifically, we wish to test that the Profile method on MyController
returns a ViewResult with the correct Model (the current user's name, to make things
interesing).
&lt;/p&gt;
&lt;p&gt;
Here's the entire test. It may look more complicated than it is – it's really only
10 lines of code, but I had to break them up to prevent unpleasant wrapping if your
screen is narrow.
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ProfileWillReturnResultWithCorrectUserName()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Customize&amp;lt;&lt;span style="color: #2b91af"&gt;ControllerContext&lt;/span&gt;&amp;gt;(ob
=&amp;gt; ob&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .OmitAutoProperties()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(cc =&amp;gt; cc.HttpContext));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; expectedUserName
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous(&lt;span style="color: #a31515"&gt;"UserName"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; httpCtxStub
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; httpCtxStub.SetupGet(x =&amp;gt; x.User).Returns(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;GenericPrincipal&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;GenericIdentity&lt;/span&gt;(expectedUserName), &lt;span style="color: blue"&gt;null&lt;/span&gt;));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register(httpCtxStub.Object);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyController&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .OmitAutoProperties()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(c =&amp;gt; c.ControllerContext)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CreateAnonymous();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ViewResult&lt;/span&gt; result
= sut.Profile();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; actual
= result.ViewData.Model;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(expectedUserName,
actual, &lt;span style="color: #a31515"&gt;"User"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Apart from AutoFixture, I'm also making use of &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; to
stub out HttpContextBase.
&lt;/p&gt;
&lt;p&gt;
You can see the Anonymous With method in two different places: in the call to &lt;a href="http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx"&gt;Customize&lt;/a&gt; and
when the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; is being built. In both
cases you can see that the call to With follows a call to OmitAutoProperties. In other
words: we are telling AutoFixture that we don't want any of the writable properties
to be assigned a value &lt;em&gt;except&lt;/em&gt; the one we identify.
&lt;/p&gt;
&lt;p&gt;
Let me highlight some parts of the test.
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;fixture.Customize&amp;lt;&lt;span style="color: #2b91af"&gt;ControllerContext&lt;/span&gt;&amp;gt;(ob
=&amp;gt; ob&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .OmitAutoProperties()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(cc =&amp;gt; cc.HttpContext));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This line of code instructs AutoFixture to always create a ControllerContext in a
particular way: I don't want to use AutoProperties here, because ControllerContext
has a lot of writable properties of abstract types, and that would require me to set
up a lot of Test Doubles if I had to assign values to all of those. It's much easier
to simply opt out of this mechanism. However, I &lt;em&gt;would&lt;/em&gt; like to have the HttpContext
property assigned, but I don't care about the value in this statement, so the With
method simply states that AutoFixture should assign a value according to whatever
rule it has for creating instances of HttpContextBase.
&lt;/p&gt;
&lt;p&gt;
I can now set up a Stub that populates the User property of HttpContextBase:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; httpCtxStub
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;httpCtxStub.SetupGet(x =&amp;gt; x.User).Returns(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;GenericPrincipal&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;GenericIdentity&lt;/span&gt;(expectedUserName), &lt;span style="color: blue"&gt;null&lt;/span&gt;));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.Register(httpCtxStub.Object);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This is registered with the fixture instance which closes the loop to the previous
customization.
&lt;/p&gt;
&lt;p&gt;
I can now create an instance of my SUT. Once more, I don't want to have to set up
a lot of irrelevant properties on MyController, so I opt out of AutoProperties and
then explicitly opt in on the ControllerContext. This will cause AutoFixture to automatically
populate the ControllerContext with the HttpContext Stub:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyController&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .OmitAutoProperties()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(c =&amp;gt; c.ControllerContext)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CreateAnonymous();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
For completeness' sake, here's the MyController class that I am testing:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyController&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;Controller&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ViewResult&lt;/span&gt; Profile()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;object&lt;/span&gt; userName
= &lt;span style="color: blue"&gt;this&lt;/span&gt;.User.Identity.Name;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.View(userName);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This test may seem complex, but it really accomplishes a lot in only 10 lines of code,
considering that ASP.NET MVC Controllers with a working HttpContext are not particularly
trivial to set up.
&lt;/p&gt;
&lt;p&gt;
In summary, this With method overload lets you opt in on one or more explicitly identified
properties when you have otherwise decided to omit AutoProperties. As all the other
Builder methods, this method can also be chained.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=43e6e4e3-6e34-4100-9106-3c9fa12b9e8c" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,43e6e4e3-6e34-4100-9106-3c9fa12b9e8c.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=be02d84b-712f-4d40-ad4f-66e34bed4fc9</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,be02d84b-712f-4d40-ad4f-66e34bed4fc9.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,be02d84b-712f-4d40-ad4f-66e34bed4fc9.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=be02d84b-712f-4d40-ad4f-66e34bed4fc9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you are doing Rich UI, <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx">INotifyPropertyChanged</a> is
a pretty important interface. This is as true for WPF as it was for Windows Forms.
Consisting solely of an event, it's <a href="http://blogs.msdn.com/ploeh/archive/2006/01/19/TestingEventsUsingAnonymousMethods.aspx">not
any harder to unit test than other events</a>.
</p>
        <p>
You can certainly write each test manually like the following.
</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  ChangingMyPropertyWillRaiseNotifyEvent_Classic()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 bool\cf0  eventWasRaised = \cf4 false\cf0 ;\par ??    \cf4 var\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    sut.PropertyChanged += (sender, e) =&gt;\par ??        \{\par ??            \cf4 if\cf0  (e.PropertyName == \cf6 "MyProperty"\cf0 )\par ??            \{\par ??                eventWasRaised = \cf4 true\cf0 ;\par ??            \}\par ??        \};\par ??    \cf5 // Exercise system\par ??\cf0     sut.MyProperty = \cf6 "Some new value"\cf0 ;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .IsTrue(eventWasRaised, \cf6 "Event was raised"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> ChangingMyPropertyWillRaiseNotifyEvent_Classic()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">bool</span> eventWasRaised
= <span style="color: blue">false</span>;</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    sut.PropertyChanged += (sender, e) =&gt;</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">if</span> (e.PropertyName
== <span style="color: #a31515">"MyProperty"</span>)</pre>
          <pre style="margin: 0px">            {</pre>
          <pre style="margin: 0px">                eventWasRaised = <span style="color: blue">true</span>;</pre>
          <pre style="margin: 0px">            }</pre>
          <pre style="margin: 0px">        };</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    sut.MyProperty = <span style="color: #a31515">"Some
new value"</span>;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.IsTrue(eventWasRaised, <span style="color: #a31515">"Event
was raised"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Even for a one-off test, this one has a few problems. From an <a href="http://xunitpatterns.com/"><em>xUnit
Test Patterns</em></a> point of view, there's the issue that the test contains conditional
logic, but that aside, the main problem is that if you have a lot of properties, writing
all these very similar tests become old hat very soon.
</p>
        <p>
To make testing INotifyPropertyChanged events easier, I created a simple fluent interface
that allows me to write the same test 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  ChangingMyPropertyWillRaiseNotifyEvent_Fluent()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system and verify outcome\par ??\cf0     sut.ShouldNotifyOn(s =&gt; s.MyProperty)\par ??        .When(s =&gt; s.MyProperty = \cf6 "Some new value"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> ChangingMyPropertyWillRaiseNotifyEvent_Fluent()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system and verify outcome</span></pre>
          <pre style="margin: 0px">    sut.ShouldNotifyOn(s =&gt; s.MyProperty)</pre>
          <pre style="margin: 0px">        .When(s =&gt; s.MyProperty = <span style="color: #a31515">"Some
new value"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
You simply state for which property you want to verify the event when a certain operation
is invoked. This is certainly more concise and intention-revealing than the previous
test.
</p>
        <p>
If you have interdependent properties, you can specify than an event was raised when
another property was modified.
</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  ChangingMyPropertyWillRaiseNotifyForDerived()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system and verify outcome\par ??\cf0     sut.ShouldNotifyOn(s =&gt; s.MyDerivedProperty)\par ??        .When(s =&gt; s.MyProperty = \cf6 "Some new value"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> ChangingMyPropertyWillRaiseNotifyForDerived()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system and verify outcome</span></pre>
          <pre style="margin: 0px">    sut.ShouldNotifyOn(s =&gt; s.MyDerivedProperty)</pre>
          <pre style="margin: 0px">        .When(s =&gt; s.MyProperty = <span style="color: #a31515">"Some
new value"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The When method takes any Action&lt;T&gt;, so you can also invoke methods, use Closures
and what not.
</p>
        <p>
There's also a <em>ShouldNotNotifyOn</em> method to verify that an event was <em>not</em> raised
when a particular operation was invoked.
</p>
        <p>
This fluent interface is implemented with an extension method on INotifyPropertyChanged,
combined with a custom class that performs the verification. Here are the extension
methods:
</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 public\cf0  \cf1 static\cf0  \cf1 class\cf0  \cf4 NotifyPropertyChanged\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf4 NotifyExpectation\cf0 &lt;T&gt;\par ??        ShouldNotifyOn&lt;T, TProperty&gt;(\cf1 this\cf0  T owner,\par ??        \cf4 Expression\cf0 &lt;\cf4 Func\cf0 &lt;T, TProperty&gt;&gt; propertyPicker) \par ??        \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0     \{\par ??        \cf1 return\cf0  \cf4 NotifyPropertyChanged\cf0 .CreateExpectation(owner,\par ??            propertyPicker, \cf1 true\cf0 );\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf4 NotifyExpectation\cf0 &lt;T&gt; \par ??        ShouldNotNotifyOn&lt;T, TProperty&gt;(\cf1 this\cf0  T owner,\par ??        \cf4 Expression\cf0 &lt;\cf4 Func\cf0 &lt;T, TProperty&gt;&gt; propertyPicker)\par ??        \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0     \{\par ??        \cf1 return\cf0  \cf4 NotifyPropertyChanged\cf0 .CreateExpectation(owner,\par ??            propertyPicker, \cf1 false\cf0 );\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf1 static\cf0  \cf4 NotifyExpectation\cf0 &lt;T&gt;\par ??        CreateExpectation&lt;T, TProperty&gt;(T owner,\par ??        \cf4 Expression\cf0 &lt;\cf4 Func\cf0 &lt;T, TProperty&gt;&gt; pickProperty,\par ??        \cf1 bool\cf0  eventExpected) \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0     \{\par ??        \cf1 string\cf0  propertyName =\par ??            ((\cf4 MemberExpression\cf0 )pickProperty.Body).Member.Name;\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf4 NotifyExpectation\cf0 &lt;T&gt;(owner,\par ??            propertyName, eventExpected);\par ??    \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">static</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">NotifyPropertyChanged</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">static</span><span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt;</pre>
          <pre style="margin: 0px">        ShouldNotifyOn&lt;T, TProperty&gt;(<span style="color: blue">this</span> T
owner,</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T,
TProperty&gt;&gt; propertyPicker) </pre>
          <pre style="margin: 0px">        <span style="color: blue">where</span> T
: <span style="color: #2b91af">INotifyPropertyChanged</span></pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span><span style="color: #2b91af">NotifyPropertyChanged</span>.CreateExpectation(owner,</pre>
          <pre style="margin: 0px">            propertyPicker, <span style="color: blue">true</span>);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">static</span><span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt; </pre>
          <pre style="margin: 0px">        ShouldNotNotifyOn&lt;T, TProperty&gt;(<span style="color: blue">this</span> T
owner,</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T,
TProperty&gt;&gt; propertyPicker)</pre>
          <pre style="margin: 0px">        <span style="color: blue">where</span> T
: <span style="color: #2b91af">INotifyPropertyChanged</span></pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span><span style="color: #2b91af">NotifyPropertyChanged</span>.CreateExpectation(owner,</pre>
          <pre style="margin: 0px">            propertyPicker, <span style="color: blue">false</span>);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">static</span><span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt;</pre>
          <pre style="margin: 0px">        CreateExpectation&lt;T, TProperty&gt;(T owner,</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T,
TProperty&gt;&gt; pickProperty,</pre>
          <pre style="margin: 0px">        <span style="color: blue">bool</span> eventExpected) <span style="color: blue">where</span> T
: <span style="color: #2b91af">INotifyPropertyChanged</span></pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">string</span> propertyName
=</pre>
          <pre style="margin: 0px">            ((<span style="color: #2b91af">MemberExpression</span>)pickProperty.Body).Member.Name;</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span><span style="color: blue">new</span><span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt;(owner,</pre>
          <pre style="margin: 0px">            propertyName, eventExpected);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
And here's the NotifyExpectation class returned by both extension methods:
</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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 NotifyExpectation\cf0 &lt;T&gt;\par ??    \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  T owner;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 string\cf0  propertyName;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 bool\cf0  eventExpected;\par ??\par ??    \cf1 public\cf0  NotifyExpectation(T owner,\par ??        \cf1 string\cf0  propertyName, \cf1 bool\cf0  eventExpected)\par ??    \{\par ??        \cf1 this\cf0 .owner = owner;\par ??        \cf1 this\cf0 .propertyName = propertyName;\par ??        \cf1 this\cf0 .eventExpected = eventExpected;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  When(\cf4 Action\cf0 &lt;T&gt; action)\par ??    \{\par ??        \cf1 bool\cf0  eventWasRaised = \cf1 false\cf0 ;\par ??        \cf1 this\cf0 .owner.PropertyChanged += (sender, e) =&gt;\par ??        \{\par ??            \cf1 if\cf0  (e.PropertyName == \cf1 this\cf0 .propertyName)\par ??            \{\par ??                eventWasRaised = \cf1 true\cf0 ;\par ??            \}\par ??        \};\par ??        action(\cf1 this\cf0 .owner);\par ??\par ??        \cf4 Assert\cf0 .AreEqual&lt;\cf1 bool\cf0 &gt;(\cf1 this\cf0 .eventExpected,\par ??            eventWasRaised,\par ??            \cf5 "PropertyChanged on \{0\}"\cf0 , \cf1 this\cf0 .propertyName);\par ??    \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt;</pre>
          <pre style="margin: 0px">    <span style="color: blue">where</span> T
: <span style="color: #2b91af">INotifyPropertyChanged</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span> T
owner;</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: blue">string</span> propertyName;</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: blue">bool</span> eventExpected;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> NotifyExpectation(T
owner,</pre>
          <pre style="margin: 0px">        <span style="color: blue">string</span> propertyName, <span style="color: blue">bool</span> eventExpected)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.owner
= owner;</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.propertyName
= propertyName;</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.eventExpected
= eventExpected;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">void</span> When(<span style="color: #2b91af">Action</span>&lt;T&gt;
action)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">bool</span> eventWasRaised
= <span style="color: blue">false</span>;</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.owner.PropertyChanged
+= (sender, e) =&gt;</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">if</span> (e.PropertyName
== <span style="color: blue">this</span>.propertyName)</pre>
          <pre style="margin: 0px">            {</pre>
          <pre style="margin: 0px">                eventWasRaised = <span style="color: blue">true</span>;</pre>
          <pre style="margin: 0px">            }</pre>
          <pre style="margin: 0px">        };</pre>
          <pre style="margin: 0px">        action(<span style="color: blue">this</span>.owner);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">bool</span>&gt;(<span style="color: blue">this</span>.eventExpected,</pre>
          <pre style="margin: 0px">            eventWasRaised,</pre>
          <pre style="margin: 0px">            <span style="color: #a31515">"PropertyChanged
on {0}"</span>, <span style="color: blue">this</span>.propertyName);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
You can replace the Assertion with one that matches your test framework of choice
(this one was written for MSTest).
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=be02d84b-712f-4d40-ad4f-66e34bed4fc9" />
      </body>
      <title>A Fluent Interface For Testing INotifyPropertyChanged</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,be02d84b-712f-4d40-ad4f-66e34bed4fc9.aspx</guid>
      <link>http://blog.ploeh.dk/2009/08/06/AFluentInterfaceForTestingINotifyPropertyChanged.aspx</link>
      <pubDate>Thu, 06 Aug 2009 17:58:36 GMT</pubDate>
      <description>&lt;p&gt;
If you are doing Rich UI, &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; is
a pretty important interface. This is as true for WPF as it was for Windows Forms.
Consisting solely of an event, it's &lt;a href="http://blogs.msdn.com/ploeh/archive/2006/01/19/TestingEventsUsingAnonymousMethods.aspx"&gt;not
any harder to unit test than other events&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
You can certainly write each test manually like the following.
&lt;/p&gt;
&lt;!--
{\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  ChangingMyPropertyWillRaiseNotifyEvent_Classic()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 bool\cf0  eventWasRaised = \cf4 false\cf0 ;\par ??    \cf4 var\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    sut.PropertyChanged += (sender, e) =&amp;gt;\par ??        \{\par ??            \cf4 if\cf0  (e.PropertyName == \cf6 "MyProperty"\cf0 )\par ??            \{\par ??                eventWasRaised = \cf4 true\cf0 ;\par ??            \}\par ??        \};\par ??    \cf5 // Exercise system\par ??\cf0     sut.MyProperty = \cf6 "Some new value"\cf0 ;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .IsTrue(eventWasRaised, \cf6 "Event was raised"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ChangingMyPropertyWillRaiseNotifyEvent_Classic()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;bool&lt;/span&gt; eventWasRaised
= &lt;span style="color: blue"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.PropertyChanged += (sender, e) =&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (e.PropertyName
== &lt;span style="color: #a31515"&gt;"MyProperty"&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; eventWasRaised = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.MyProperty = &lt;span style="color: #a31515"&gt;"Some
new value"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(eventWasRaised, &lt;span style="color: #a31515"&gt;"Event
was raised"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Even for a one-off test, this one has a few problems. From an &lt;a href="http://xunitpatterns.com/"&gt;&lt;em&gt;xUnit
Test Patterns&lt;/em&gt;&lt;/a&gt; point of view, there's the issue that the test contains conditional
logic, but that aside, the main problem is that if you have a lot of properties, writing
all these very similar tests become old hat very soon.
&lt;/p&gt;
&lt;p&gt;
To make testing INotifyPropertyChanged events easier, I created a simple fluent interface
that allows me to write the same test like this:
&lt;/p&gt;
&lt;!--
{\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  ChangingMyPropertyWillRaiseNotifyEvent_Fluent()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system and verify outcome\par ??\cf0     sut.ShouldNotifyOn(s =&amp;gt; s.MyProperty)\par ??        .When(s =&amp;gt; s.MyProperty = \cf6 "Some new value"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ChangingMyPropertyWillRaiseNotifyEvent_Fluent()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system and verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.ShouldNotifyOn(s =&amp;gt; s.MyProperty)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .When(s =&amp;gt; s.MyProperty = &lt;span style="color: #a31515"&gt;"Some
new value"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
You simply state for which property you want to verify the event when a certain operation
is invoked. This is certainly more concise and intention-revealing than the previous
test.
&lt;/p&gt;
&lt;p&gt;
If you have interdependent properties, you can specify than an event was raised when
another property was modified.
&lt;/p&gt;
&lt;!--
{\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  ChangingMyPropertyWillRaiseNotifyForDerived()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system and verify outcome\par ??\cf0     sut.ShouldNotifyOn(s =&amp;gt; s.MyDerivedProperty)\par ??        .When(s =&amp;gt; s.MyProperty = \cf6 "Some new value"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ChangingMyPropertyWillRaiseNotifyForDerived()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system and verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.ShouldNotifyOn(s =&amp;gt; s.MyDerivedProperty)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .When(s =&amp;gt; s.MyProperty = &lt;span style="color: #a31515"&gt;"Some
new value"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The When method takes any Action&amp;lt;T&amp;gt;, so you can also invoke methods, use Closures
and what not.
&lt;/p&gt;
&lt;p&gt;
There's also a &lt;em&gt;ShouldNotNotifyOn&lt;/em&gt; method to verify that an event was &lt;em&gt;not&lt;/em&gt; raised
when a particular operation was invoked.
&lt;/p&gt;
&lt;p&gt;
This fluent interface is implemented with an extension method on INotifyPropertyChanged,
combined with a custom class that performs the verification. Here are the extension
methods:
&lt;/p&gt;
&lt;!--
{\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 public\cf0  \cf1 static\cf0  \cf1 class\cf0  \cf4 NotifyPropertyChanged\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf4 NotifyExpectation\cf0 &amp;lt;T&amp;gt;\par ??        ShouldNotifyOn&amp;lt;T, TProperty&amp;gt;(\cf1 this\cf0  T owner,\par ??        \cf4 Expression\cf0 &amp;lt;\cf4 Func\cf0 &amp;lt;T, TProperty&amp;gt;&amp;gt; propertyPicker) \par ??        \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0     \{\par ??        \cf1 return\cf0  \cf4 NotifyPropertyChanged\cf0 .CreateExpectation(owner,\par ??            propertyPicker, \cf1 true\cf0 );\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf4 NotifyExpectation\cf0 &amp;lt;T&amp;gt; \par ??        ShouldNotNotifyOn&amp;lt;T, TProperty&amp;gt;(\cf1 this\cf0  T owner,\par ??        \cf4 Expression\cf0 &amp;lt;\cf4 Func\cf0 &amp;lt;T, TProperty&amp;gt;&amp;gt; propertyPicker)\par ??        \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0     \{\par ??        \cf1 return\cf0  \cf4 NotifyPropertyChanged\cf0 .CreateExpectation(owner,\par ??            propertyPicker, \cf1 false\cf0 );\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf1 static\cf0  \cf4 NotifyExpectation\cf0 &amp;lt;T&amp;gt;\par ??        CreateExpectation&amp;lt;T, TProperty&amp;gt;(T owner,\par ??        \cf4 Expression\cf0 &amp;lt;\cf4 Func\cf0 &amp;lt;T, TProperty&amp;gt;&amp;gt; pickProperty,\par ??        \cf1 bool\cf0  eventExpected) \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0     \{\par ??        \cf1 string\cf0  propertyName =\par ??            ((\cf4 MemberExpression\cf0 )pickProperty.Body).Member.Name;\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf4 NotifyExpectation\cf0 &amp;lt;T&amp;gt;(owner,\par ??            propertyName, eventExpected);\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyPropertyChanged&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyExpectation&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ShouldNotifyOn&amp;lt;T, TProperty&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; T
owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,
TProperty&amp;gt;&amp;gt; propertyPicker) &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; T
: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyPropertyChanged&lt;/span&gt;.CreateExpectation(owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; propertyPicker, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyExpectation&lt;/span&gt;&amp;lt;T&amp;gt; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ShouldNotNotifyOn&amp;lt;T, TProperty&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; T
owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,
TProperty&amp;gt;&amp;gt; propertyPicker)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; T
: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyPropertyChanged&lt;/span&gt;.CreateExpectation(owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; propertyPicker, &lt;span style="color: blue"&gt;false&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyExpectation&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CreateExpectation&amp;lt;T, TProperty&amp;gt;(T owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,
TProperty&amp;gt;&amp;gt; pickProperty,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;bool&lt;/span&gt; eventExpected) &lt;span style="color: blue"&gt;where&lt;/span&gt; T
: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; propertyName
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;span style="color: #2b91af"&gt;MemberExpression&lt;/span&gt;)pickProperty.Body).Member.Name;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyExpectation&lt;/span&gt;&amp;lt;T&amp;gt;(owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; propertyName, eventExpected);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
And here's the NotifyExpectation class returned by both extension methods:
&lt;/p&gt;
&lt;!--
{\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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 NotifyExpectation\cf0 &amp;lt;T&amp;gt;\par ??    \cf1 where\cf0  T : \cf4 INotifyPropertyChanged\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  T owner;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 string\cf0  propertyName;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 bool\cf0  eventExpected;\par ??\par ??    \cf1 public\cf0  NotifyExpectation(T owner,\par ??        \cf1 string\cf0  propertyName, \cf1 bool\cf0  eventExpected)\par ??    \{\par ??        \cf1 this\cf0 .owner = owner;\par ??        \cf1 this\cf0 .propertyName = propertyName;\par ??        \cf1 this\cf0 .eventExpected = eventExpected;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  When(\cf4 Action\cf0 &amp;lt;T&amp;gt; action)\par ??    \{\par ??        \cf1 bool\cf0  eventWasRaised = \cf1 false\cf0 ;\par ??        \cf1 this\cf0 .owner.PropertyChanged += (sender, e) =&amp;gt;\par ??        \{\par ??            \cf1 if\cf0  (e.PropertyName == \cf1 this\cf0 .propertyName)\par ??            \{\par ??                eventWasRaised = \cf1 true\cf0 ;\par ??            \}\par ??        \};\par ??        action(\cf1 this\cf0 .owner);\par ??\par ??        \cf4 Assert\cf0 .AreEqual&amp;lt;\cf1 bool\cf0 &amp;gt;(\cf1 this\cf0 .eventExpected,\par ??            eventWasRaised,\par ??            \cf5 "PropertyChanged on \{0\}"\cf0 , \cf1 this\cf0 .propertyName);\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotifyExpectation&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; T
: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; T
owner;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; propertyName;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; eventExpected;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; NotifyExpectation(T
owner,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; propertyName, &lt;span style="color: blue"&gt;bool&lt;/span&gt; eventExpected)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.owner
= owner;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.propertyName
= propertyName;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.eventExpected
= eventExpected;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; When(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;
action)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;bool&lt;/span&gt; eventWasRaised
= &lt;span style="color: blue"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.owner.PropertyChanged
+= (sender, e) =&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (e.PropertyName
== &lt;span style="color: blue"&gt;this&lt;/span&gt;.propertyName)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; eventWasRaised = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; action(&lt;span style="color: blue"&gt;this&lt;/span&gt;.owner);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.eventExpected,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; eventWasRaised,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"PropertyChanged
on {0}"&lt;/span&gt;, &lt;span style="color: blue"&gt;this&lt;/span&gt;.propertyName);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
You can replace the Assertion with one that matches your test framework of choice
(this one was written for MSTest).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=be02d84b-712f-4d40-ad4f-66e34bed4fc9" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,be02d84b-712f-4d40-ad4f-66e34bed4fc9.aspx</comments>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=d80f148c-cf4e-45fe-955f-02418a93bcde</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,d80f148c-cf4e-45fe-955f-02418a93bcde.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,d80f148c-cf4e-45fe-955f-02418a93bcde.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=d80f148c-cf4e-45fe-955f-02418a93bcde</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Since <a href="http://autofixture.codeplex.com/">AutoFixture</a><a href="http://blog.ploeh.dk/2009/07/01/AutoFixtureAsTestDataBuilder.aspx">is
a Test Data Builder</a>, one of its most important tasks is to build up graphs of
fully populated, yet semantically correct, strongly typed objects. As such, its default
behavior is to assign a value to every writable property in the object graph.
</p>
        <p>
While this is sometimes the desired behavior, at other times it is not.
</p>
        <p>
This is particularly the case when you want to test that a newly created object has
a property of a particular value. When you want to test the default value of a writable
property, AutoFixture's AutoProperty feature is very much in the way.
</p>
        <p>
Let's consider as an example a piece of software that deals with vehicle registration.
By default, a vehicle should have four wheels, since this is the most common occurrence.
Although I always practice TDD, I'll start by showing you the Vehicle class to illustrate
what I mean.
</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 public\cf0  \cf1 class\cf0  \cf4 Vehicle\par ??\cf0 \{\par ??    \cf1 public\cf0  Vehicle()\par ??    \{\par ??        \cf1 this\cf0 .Wheels = 4;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 int\cf0  Wheels \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">Vehicle</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> Vehicle()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.Wheels
= 4;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">int</span> Wheels
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Here's a test that ensures that the default number of wheels is <em>4</em> – or does
it?
</p>
        <p>
In fact the assertion fails because the actual value is <em>1</em>, not <em>4</em>.
</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  AnonymousVehicleHasWheelsAssignedByFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&lt;\cf3 Vehicle\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 var\cf0  result = sut.Wheels;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(4, result, \cf6 "Wheels"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> AnonymousVehicleHasWheelsAssignedByFixture()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Vehicle</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> result
= sut.Wheels;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(4,
result, <span style="color: #a31515">"Wheels"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Why does the test fail when the value of Wheels is set to <em>4</em> in the constructor?
It fails because AutoFixture is designed to create <em>populated</em> test data, so
it assigns a value to every writable property. Wheels is a writable property, so AutoFixture
assigns an integer value to it using its <a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx">default
algorithm for creating anonymous numbers</a>. Since no other numbers are being created
during this test, the number assigned to Wheels is <em>1</em>. This is AutoFixture's
AutoProperties feature in effect.
</p>
        <p>
When you want to test constructor logic, or otherwise wish to disable the AutoProperties
feature, you can use a <a href="http://blog.ploeh.dk/2009/05/26/TheAutoFixtureBuilder.aspx">customized
Builder</a> with the OmitAutoProperties method:
</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  VehicleWithoutAutoPropertiesWillHaveFourWheels()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf4 var\cf0  sut = fixture.Build&lt;\cf3 Vehicle\cf0 &gt;()\par ??        .OmitAutoProperties()\par ??        .CreateAnonymous();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 var\cf0  result = sut.Wheels;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(4, result, \cf6 "Wheels"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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> VehicleWithoutAutoPropertiesWillHaveFourWheels()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> sut
= fixture.Build&lt;<span style="color: #2b91af">Vehicle</span>&gt;()</pre>
          <pre style="margin: 0px">        .OmitAutoProperties()</pre>
          <pre style="margin: 0px">        .CreateAnonymous();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> result
= sut.Wheels;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(4,
result, <span style="color: #a31515">"Wheels"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The OmitAutoProperties method instructs AutoFixture to skip assigning automatic Anonymous
Values to all writable properties in the object graph. Any properties specifically
assigned by the <a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx">With
method</a> will still be assigned.
</p>
        <p>
The test using OmitAutoProperties succeeds.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=d80f148c-cf4e-45fe-955f-02418a93bcde" />
      </body>
      <title>Disabling AutoProperties In AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,d80f148c-cf4e-45fe-955f-02418a93bcde.aspx</guid>
      <link>http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx</link>
      <pubDate>Thu, 23 Jul 2009 14:54:45 GMT</pubDate>
      <description>&lt;p&gt;
Since &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; &lt;a href="http://blog.ploeh.dk/2009/07/01/AutoFixtureAsTestDataBuilder.aspx"&gt;is
a Test Data Builder&lt;/a&gt;, one of its most important tasks is to build up graphs of
fully populated, yet semantically correct, strongly typed objects. As such, its default
behavior is to assign a value to every writable property in the object graph.
&lt;/p&gt;
&lt;p&gt;
While this is sometimes the desired behavior, at other times it is not.
&lt;/p&gt;
&lt;p&gt;
This is particularly the case when you want to test that a newly created object has
a property of a particular value. When you want to test the default value of a writable
property, AutoFixture's AutoProperty feature is very much in the way.
&lt;/p&gt;
&lt;p&gt;
Let's consider as an example a piece of software that deals with vehicle registration.
By default, a vehicle should have four wheels, since this is the most common occurrence.
Although I always practice TDD, I'll start by showing you the Vehicle class to illustrate
what I mean.
&lt;/p&gt;
&lt;!--
{\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 public\cf0  \cf1 class\cf0  \cf4 Vehicle\par ??\cf0 \{\par ??    \cf1 public\cf0  Vehicle()\par ??    \{\par ??        \cf1 this\cf0 .Wheels = 4;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 int\cf0  Wheels \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Vehicle&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; Vehicle()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Wheels
= 4;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; Wheels
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Here's a test that ensures that the default number of wheels is &lt;em&gt;4&lt;/em&gt; – or does
it?
&lt;/p&gt;
&lt;p&gt;
In fact the assertion fails because the actual value is &lt;em&gt;1&lt;/em&gt;, not &lt;em&gt;4&lt;/em&gt;.
&lt;/p&gt;
&lt;!--
{\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  AnonymousVehicleHasWheelsAssignedByFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf4 var\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 Vehicle\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 var\cf0  result = sut.Wheels;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(4, result, \cf6 "Wheels"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AnonymousVehicleHasWheelsAssignedByFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Vehicle&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; result
= sut.Wheels;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(4,
result, &lt;span style="color: #a31515"&gt;"Wheels"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Why does the test fail when the value of Wheels is set to &lt;em&gt;4&lt;/em&gt; in the constructor?
It fails because AutoFixture is designed to create &lt;em&gt;populated&lt;/em&gt; test data, so
it assigns a value to every writable property. Wheels is a writable property, so AutoFixture
assigns an integer value to it using its &lt;a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx"&gt;default
algorithm for creating anonymous numbers&lt;/a&gt;. Since no other numbers are being created
during this test, the number assigned to Wheels is &lt;em&gt;1&lt;/em&gt;. This is AutoFixture's
AutoProperties feature in effect.
&lt;/p&gt;
&lt;p&gt;
When you want to test constructor logic, or otherwise wish to disable the AutoProperties
feature, you can use a &lt;a href="http://blog.ploeh.dk/2009/05/26/TheAutoFixtureBuilder.aspx"&gt;customized
Builder&lt;/a&gt; with the OmitAutoProperties method:
&lt;/p&gt;
&lt;!--
{\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  VehicleWithoutAutoPropertiesWillHaveFourWheels()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 var\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf4 var\cf0  sut = fixture.Build&amp;lt;\cf3 Vehicle\cf0 &amp;gt;()\par ??        .OmitAutoProperties()\par ??        .CreateAnonymous();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 var\cf0  result = sut.Wheels;\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(4, result, \cf6 "Wheels"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; VehicleWithoutAutoPropertiesWillHaveFourWheels()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;Vehicle&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .OmitAutoProperties()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CreateAnonymous();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; result
= sut.Wheels;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(4,
result, &lt;span style="color: #a31515"&gt;"Wheels"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The OmitAutoProperties method instructs AutoFixture to skip assigning automatic Anonymous
Values to all writable properties in the object graph. Any properties specifically
assigned by the &lt;a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx"&gt;With
method&lt;/a&gt; will still be assigned.
&lt;/p&gt;
&lt;p&gt;
The test using OmitAutoProperties succeeds.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=d80f148c-cf4e-45fe-955f-02418a93bcde" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,d80f148c-cf4e-45fe-955f-02418a93bcde.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=20eacbdc-fcb2-4dc0-9024-c3735cb762a3</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,20eacbdc-fcb2-4dc0-9024-c3735cb762a3.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,20eacbdc-fcb2-4dc0-9024-c3735cb762a3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=20eacbdc-fcb2-4dc0-9024-c3735cb762a3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some time ago, my good friend Martin Gildenpfennig from <a href="http://www.ative.dk/">Ative</a> made
me aware that <a href="http://autofixture.codeplex.com/">AutoFixture</a> (among other
things) is an generic implementation of the <a href="http://www.natpryce.com/articles/000714.html">Test
Data Builder</a> pattern, and indeed it is.
</p>
        <p>
In the original Gang of Four definition of a Design Pattern, several people must independently
have arrived at the same general solution to a given problem before we can call a
coding idiom a true Pattern. In this spirit, the Test Data Builder pattern is on the
verge of becoming a true Design Pattern, since I came up with AutoFixture without
having heard about Test Data Builder :)
</p>
        <p>
The problem is the same: How do we create semantically correct test objects in a manner
that is flexible and yet hides away irrelevant complexity?
</p>
        <p>
Like others before me, I tried the Object Mother (anti?)pattern and found it lacking.
To me the answer was AutoFixture, a library that heuristically builds object graphs
using Reflection and built-in rules for specific types.
</p>
        <p>
Although my original approach was different, you can certainly use AutoFixture as
a generic Test Data Builder.
</p>
        <p>
To demonstrate this, let's work with this (oversimplified) Order object model:
</p>
        <p>
          <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/AutoFixtureAsTestDataBuilder_1371F/image_3.png" width="478" height="457" />
        </p>
        <p>
Assuming that we have an instance of the Fixture class (called <em>fixture</em>),
we can create a new instance of the Order class with a ShippingAddress in Denmark:
</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;\red163\green21\blue21;}??\fs20 \cf1 var\cf0  order = fixture.Build&lt;\cf4 Order\cf0 &gt;()\par ??    .With(o =&gt; o.ShippingAddress, \par ??        fixture.Build&lt;\cf4 Address\cf0 &gt;()\par ??        .With(a =&gt; a.Country, \cf5 "Denmark"\cf0 )\par ??        .CreateAnonymous())\par ??    .CreateAnonymous();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> order
= fixture.Build&lt;<span style="color: #2b91af">Order</span>&gt;()</pre>
          <pre style="margin: 0px">    .With(o =&gt; o.ShippingAddress, </pre>
          <pre style="margin: 0px">        fixture.Build&lt;<span style="color: #2b91af">Address</span>&gt;()</pre>
          <pre style="margin: 0px">        .With(a =&gt; a.Country, <span style="color: #a31515">"Denmark"</span>)</pre>
          <pre style="margin: 0px">        .CreateAnonymous())</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
While this works and follows the Test Data Builder pattern, I find this more concise
and readable:
</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;\red163\green21\blue21;}??\fs20 \cf1 var\cf0  order = fixture.CreateAnonymous&lt;\cf4 Order\cf0 &gt;();\par ??order.ShippingAddress.Country = \cf5 "Denmark"\cf0 ;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> order
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Order</span>&gt;();</pre>
          <pre style="margin: 0px">order.ShippingAddress.Country = <span style="color: #a31515">"Denmark"</span>;</pre>
        </div>
        <p>
The result is the same.
</p>
        <p>
We can also add anonymous order lines to the order using this fluent interface:
</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 var\cf0  order = fixture.Build&lt;\cf4 Order\cf0 &gt;()\par ??    .Do(o =&gt; fixture.AddManyTo(o.OrderLines))\par ??    .CreateAnonymous();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> order
= fixture.Build&lt;<span style="color: #2b91af">Order</span>&gt;()</pre>
          <pre style="margin: 0px">    .Do(o =&gt; fixture.AddManyTo(o.OrderLines))</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
but again, I find it easier to simply let AutoFixture create a fully anonymous Order
instance, and then afterwards modify the relevant parts:
</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 var\cf0  order = fixture.CreateAnonymous&lt;\cf4 Order\cf0 &gt;();\par ??fixture.AddManyTo(order.OrderLines);}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> order
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Order</span>&gt;();</pre>
          <pre style="margin: 0px">fixture.AddManyTo(order.OrderLines);</pre>
        </div>
        <p>
Whether you prefer one style over the other, AutoFixture supports them both.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=20eacbdc-fcb2-4dc0-9024-c3735cb762a3" />
      </body>
      <title>AutoFixture As Test Data Builder</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,20eacbdc-fcb2-4dc0-9024-c3735cb762a3.aspx</guid>
      <link>http://blog.ploeh.dk/2009/07/01/AutoFixtureAsTestDataBuilder.aspx</link>
      <pubDate>Wed, 01 Jul 2009 06:19:00 GMT</pubDate>
      <description>&lt;p&gt;
Some time ago, my good friend Martin Gildenpfennig from &lt;a href="http://www.ative.dk/"&gt;Ative&lt;/a&gt; made
me aware that &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; (among other
things) is an generic implementation of the &lt;a href="http://www.natpryce.com/articles/000714.html"&gt;Test
Data Builder&lt;/a&gt; pattern, and indeed it is.
&lt;/p&gt;
&lt;p&gt;
In the original Gang of Four definition of a Design Pattern, several people must independently
have arrived at the same general solution to a given problem before we can call a
coding idiom a true Pattern. In this spirit, the Test Data Builder pattern is on the
verge of becoming a true Design Pattern, since I came up with AutoFixture without
having heard about Test Data Builder :)
&lt;/p&gt;
&lt;p&gt;
The problem is the same: How do we create semantically correct test objects in a manner
that is flexible and yet hides away irrelevant complexity?
&lt;/p&gt;
&lt;p&gt;
Like others before me, I tried the Object Mother (anti?)pattern and found it lacking.
To me the answer was AutoFixture, a library that heuristically builds object graphs
using Reflection and built-in rules for specific types.
&lt;/p&gt;
&lt;p&gt;
Although my original approach was different, you can certainly use AutoFixture as
a generic Test Data Builder.
&lt;/p&gt;
&lt;p&gt;
To demonstrate this, let's work with this (oversimplified) Order object model:
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/AutoFixtureAsTestDataBuilder_1371F/image_3.png" width="478" height="457"&gt; 
&lt;/p&gt;
&lt;p&gt;
Assuming that we have an instance of the Fixture class (called &lt;em&gt;fixture&lt;/em&gt;),
we can create a new instance of the Order class with a ShippingAddress in Denmark:
&lt;/p&gt;
&lt;!--
{\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;\red163\green21\blue21;}??\fs20 \cf1 var\cf0  order = fixture.Build&amp;lt;\cf4 Order\cf0 &amp;gt;()\par ??    .With(o =&amp;gt; o.ShippingAddress, \par ??        fixture.Build&amp;lt;\cf4 Address\cf0 &amp;gt;()\par ??        .With(a =&amp;gt; a.Country, \cf5 "Denmark"\cf0 )\par ??        .CreateAnonymous())\par ??    .CreateAnonymous();}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; order
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;Order&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(o =&amp;gt; o.ShippingAddress, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;Address&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(a =&amp;gt; a.Country, &lt;span style="color: #a31515"&gt;"Denmark"&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CreateAnonymous())&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CreateAnonymous();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
While this works and follows the Test Data Builder pattern, I find this more concise
and readable:
&lt;/p&gt;
&lt;!--
{\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;\red163\green21\blue21;}??\fs20 \cf1 var\cf0  order = fixture.CreateAnonymous&amp;lt;\cf4 Order\cf0 &amp;gt;();\par ??order.ShippingAddress.Country = \cf5 "Denmark"\cf0 ;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; order
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Order&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;order.ShippingAddress.Country = &lt;span style="color: #a31515"&gt;"Denmark"&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The result is the same.
&lt;/p&gt;
&lt;p&gt;
We can also add anonymous order lines to the order using this fluent interface:
&lt;/p&gt;
&lt;!--
{\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 var\cf0  order = fixture.Build&amp;lt;\cf4 Order\cf0 &amp;gt;()\par ??    .Do(o =&amp;gt; fixture.AddManyTo(o.OrderLines))\par ??    .CreateAnonymous();}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; order
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;Order&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Do(o =&amp;gt; fixture.AddManyTo(o.OrderLines))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .CreateAnonymous();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
but again, I find it easier to simply let AutoFixture create a fully anonymous Order
instance, and then afterwards modify the relevant parts:
&lt;/p&gt;
&lt;!--
{\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 var\cf0  order = fixture.CreateAnonymous&amp;lt;\cf4 Order\cf0 &amp;gt;();\par ??fixture.AddManyTo(order.OrderLines);}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; order
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Order&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.AddManyTo(order.OrderLines);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Whether you prefer one style over the other, AutoFixture supports them both.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=20eacbdc-fcb2-4dc0-9024-c3735cb762a3" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,20eacbdc-fcb2-4dc0-9024-c3735cb762a3.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When I talk with people about TDD and unit testing, the discussion often moves into
the area of Testability – that is, the software's susceptibility to unit testing.
A couple of years back, <a href="http://weblogs.asp.net/rosherove/default.aspx">Roy</a> even
discussed the seemingly <a href="http://weblogs.asp.net/rosherove/archive/2007/02/25/why-you-should-think-about-ootp-object-oriented-testable-programming.aspx">opposable
forces of Object-Oriented Design and Testability</a>.
</p>
        <p>
Lately, it has been occurring to me that there really isn't any conflict. Encapsulation
is important because it manifests expert knowledge so that other developers can effectively
leverage that knowledge, and it does so in a way that minimizes misuse.
</p>
        <p>
However, too much encapsulation goes against the Open/Closed Principle (that states
that objects should be open for extension, but closed for modification). From a Testability
perspective, the Open/Closed Principle pulls object-oriented design in the desired
direction. Equivalently, done correctly, making your API Testable is simply opening
it up for extensibility.
</p>
        <p>
As an example, consider a simple <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx">WPF
ViewModel</a> class called MainWindowViewModel. This class has an <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx">ICommand</a> property
that, when invoked, should show a message box. Showing a message box is good example
of breaking testability, because if the <a href="http://xunitpatterns.com/SUT.html">SUT</a> were
to show a message box, it would be very hard to automatically verify and we wouldn't
have fully automated tests.
</p>
        <p>
For this reason, we need to introduce an abstraction that basically models an action
with a string as input. <a href="http://blog.ploeh.dk/2009/05/28/DelegatesAreAnonymousInterfaces.aspx">Although
we could define an interface for that, an Action&lt;string&gt; fits the bill perfectly</a>.
</p>
        <p>
To enable that feature, I decide to use Constructor Injection to inject that abstraction
into the MainWindowViewModel class:
</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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  MainWindowViewModel(\cf4 Action\cf0 &lt;\cf1 string\cf0 &gt; notify)\par ??\{\par ??    \cf1 this\cf0 .ButtonCommand = \cf1 new\cf0  \cf4 RelayCommand\cf0 (p =&gt; \par ??    \{ notify(\cf5 "Button was clicked!"\cf0 ); \});\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> MainWindowViewModel(<span style="color: #2b91af">Action</span>&lt;<span style="color: blue">string</span>&gt;
notify)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span>.ButtonCommand
= <span style="color: blue">new</span><span style="color: #2b91af">RelayCommand</span>(p
=&gt; </pre>
          <pre style="margin: 0px">    { notify(<span style="color: #a31515">"Button
was clicked!"</span>); });</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
When I recently did that at a public talk I gave, one member of the audience initially
reacted by assuming that I was now introducing test-specific code into my SUT, but
that's not the case.
</p>
        <p>
What I'm really doing here is opening the MainWindowViewModel class for extensibility.
It can still be used with message boxes:
</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 var\cf0  vm = \cf1 new\cf0  \cf4 MainWindowViewModel\cf0 (s =&gt; \cf4 MessageBox\cf0 .Show(s));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> vm
= <span style="color: blue">new</span><span style="color: #2b91af">MainWindowViewModel</span>(s
=&gt; <span style="color: #2b91af">MessageBox</span>.Show(s));</pre>
        </div>
        <p>
but now we also have the option of notifying by sending off an email; writing to a
database; or whatever else we can think of.
</p>
        <p>
It just so happens that one of the things we can do instead of showing a message box,
is unit testing by passing in a <a href="http://xunitpatterns.com/Test%20Double.html">Test
Double</a>.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green128\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 // Fixture setup\par ??\cf3 var\cf0  mockNotify = \par ??    \cf5 MockRepository\cf0 .GenerateMock&lt;\cf5 Action\cf0 &lt;\cf3 string\cf0 &gt;&gt;();\par ??mockNotify.Expect(a =&gt; a(\cf6 "Button was clicked!"\cf0 ));\par ??\par ??\cf3 var\cf0  sut = \cf3 new\cf0  \cf5 MainWindowViewModel\cf0 (mockNotify);\par ??\cf1 // Exercise system\par ??\cf0 sut.ButtonCommand.Execute(\cf3 new\cf0  \cf3 object\cf0 ());\par ??\cf1 // Verify outcome\par ??\cf0 mockNotify.VerifyAllExpectations();\par ??\cf1 // Teardown}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: green">//
Fixture setup</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mockNotify
= </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">Action</span>&lt;<span style="color: blue">string</span>&gt;&gt;();</pre>
          <pre style="margin: 0px">mockNotify.Expect(a =&gt; a(<span style="color: #a31515">"Button
was clicked!"</span>));</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MainWindowViewModel</span>(mockNotify);</pre>
          <pre style="margin: 0px">
            <span style="color: green">//
Exercise system</span>
          </pre>
          <pre style="margin: 0px">sut.ButtonCommand.Execute(<span style="color: blue">new</span><span style="color: blue">object</span>());</pre>
          <pre style="margin: 0px">
            <span style="color: green">//
Verify outcome</span>
          </pre>
          <pre style="margin: 0px">mockNotify.VerifyAllExpectations();</pre>
          <pre style="margin: 0px">
            <span style="color: green">//
Teardown</span>
          </pre>
        </div>
        <p>
Once again, TDD has lead to better design. In this case it prompted me to open the
class for extensibility. There really isn't a need for Testability as a specific concept;
the Open/Closed Principle should be enough to drive us in the right direction.
</p>
        <p>
Pragmatically, that's not the case, so we use TDD to drive us towards the Open/Closed
Principle, but I think it's important to note that we are not only doing this to enable
testing: We are creating a better and more flexible API at the same time.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c" />
      </body>
      <title>Testability Is Really The Open/Closed Principle</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c.aspx</guid>
      <link>http://blog.ploeh.dk/2009/06/05/TestabilityIsReallyTheOpenClosedPrinciple.aspx</link>
      <pubDate>Fri, 05 Jun 2009 07:56:19 GMT</pubDate>
      <description>&lt;p&gt;
When I talk with people about TDD and unit testing, the discussion often moves into
the area of Testability – that is, the software's susceptibility to unit testing.
A couple of years back, &lt;a href="http://weblogs.asp.net/rosherove/default.aspx"&gt;Roy&lt;/a&gt; even
discussed the seemingly &lt;a href="http://weblogs.asp.net/rosherove/archive/2007/02/25/why-you-should-think-about-ootp-object-oriented-testable-programming.aspx"&gt;opposable
forces of Object-Oriented Design and Testability&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Lately, it has been occurring to me that there really isn't any conflict. Encapsulation
is important because it manifests expert knowledge so that other developers can effectively
leverage that knowledge, and it does so in a way that minimizes misuse.
&lt;/p&gt;
&lt;p&gt;
However, too much encapsulation goes against the Open/Closed Principle (that states
that objects should be open for extension, but closed for modification). From a Testability
perspective, the Open/Closed Principle pulls object-oriented design in the desired
direction. Equivalently, done correctly, making your API Testable is simply opening
it up for extensibility.
&lt;/p&gt;
&lt;p&gt;
As an example, consider a simple &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx"&gt;WPF
ViewModel&lt;/a&gt; class called MainWindowViewModel. This class has an &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx"&gt;ICommand&lt;/a&gt; property
that, when invoked, should show a message box. Showing a message box is good example
of breaking testability, because if the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; were
to show a message box, it would be very hard to automatically verify and we wouldn't
have fully automated tests.
&lt;/p&gt;
&lt;p&gt;
For this reason, we need to introduce an abstraction that basically models an action
with a string as input. &lt;a href="http://blog.ploeh.dk/2009/05/28/DelegatesAreAnonymousInterfaces.aspx"&gt;Although
we could define an interface for that, an Action&amp;lt;string&amp;gt; fits the bill perfectly&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
To enable that feature, I decide to use Constructor Injection to inject that abstraction
into the MainWindowViewModel class:
&lt;/p&gt;
&lt;!--
{\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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  MainWindowViewModel(\cf4 Action\cf0 &amp;lt;\cf1 string\cf0 &amp;gt; notify)\par ??\{\par ??    \cf1 this\cf0 .ButtonCommand = \cf1 new\cf0  \cf4 RelayCommand\cf0 (p =&amp;gt; \par ??    \{ notify(\cf5 "Button was clicked!"\cf0 ); \});\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; MainWindowViewModel(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
notify)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.ButtonCommand
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RelayCommand&lt;/span&gt;(p
=&amp;gt; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { notify(&lt;span style="color: #a31515"&gt;"Button
was clicked!"&lt;/span&gt;); });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
When I recently did that at a public talk I gave, one member of the audience initially
reacted by assuming that I was now introducing test-specific code into my SUT, but
that's not the case.
&lt;/p&gt;
&lt;p&gt;
What I'm really doing here is opening the MainWindowViewModel class for extensibility.
It can still be used with message boxes:
&lt;/p&gt;
&lt;!--
{\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 var\cf0  vm = \cf1 new\cf0  \cf4 MainWindowViewModel\cf0 (s =&amp;gt; \cf4 MessageBox\cf0 .Show(s));}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; vm
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MainWindowViewModel&lt;/span&gt;(s
=&amp;gt; &lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(s));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
but now we also have the option of notifying by sending off an email; writing to a
database; or whatever else we can think of.
&lt;/p&gt;
&lt;p&gt;
It just so happens that one of the things we can do instead of showing a message box,
is unit testing by passing in a &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test
Double&lt;/a&gt;.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green128\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 // Fixture setup\par ??\cf3 var\cf0  mockNotify = \par ??    \cf5 MockRepository\cf0 .GenerateMock&amp;lt;\cf5 Action\cf0 &amp;lt;\cf3 string\cf0 &amp;gt;&amp;gt;();\par ??mockNotify.Expect(a =&amp;gt; a(\cf6 "Button was clicked!"\cf0 ));\par ??\par ??\cf3 var\cf0  sut = \cf3 new\cf0  \cf5 MainWindowViewModel\cf0 (mockNotify);\par ??\cf1 // Exercise system\par ??\cf0 sut.ButtonCommand.Execute(\cf3 new\cf0  \cf3 object\cf0 ());\par ??\cf1 // Verify outcome\par ??\cf0 mockNotify.VerifyAllExpectations();\par ??\cf1 // Teardown}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; mockNotify
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;mockNotify.Expect(a =&amp;gt; a(&lt;span style="color: #a31515"&gt;"Button
was clicked!"&lt;/span&gt;));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MainWindowViewModel&lt;/span&gt;(mockNotify);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;sut.ButtonCommand.Execute(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;mockNotify.VerifyAllExpectations();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Once again, TDD has lead to better design. In this case it prompted me to open the
class for extensibility. There really isn't a need for Testability as a specific concept;
the Open/Closed Principle should be enough to drive us in the right direction.
&lt;/p&gt;
&lt;p&gt;
Pragmatically, that's not the case, so we use TDD to drive us towards the Open/Closed
Principle, but I think it's important to note that we are not only doing this to enable
testing: We are creating a better and more flexible API at the same time.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3cdd18ec-a1a3-4572-9a5d-4b914b0fbd7c.aspx</comments>
      <category>Software Design</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ef5bab91-8b41-4cc5-9e44-f6accd855d64</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ef5bab91-8b41-4cc5-9e44-f6accd855d64</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Dear reader, I hope you are still with me!
</p>
        <p>
After eight posts of <a href="http://autofixture.codeplex.com/">AutoFixture</a> feature
walkthroughs, I can't blame you for wondering why this tool might even be relevant
to you. In this post, we'll finally begin to look at how AutoFixture can help you
towards <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a>!
</p>
        <p>
In an earlier post, I described how the <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">Fixture
Object</a> pattern can help you greatly reduce the amount of test code that you have
to write. Since AutoFixture was designed to act as a general-purpose Fixture Object,
it can help you reduce the amount of test code even further, letting you focus on <a href="http://blog.ploeh.dk/2009/03/10/SpecificationDrivenDevelopment.aspx">specifying
the behavior</a> of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
</p>
        <p>
In that former post, the original example was this complex test that I will repeat
in it's entirety for your benefit (or horror):
</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-family: courier new; background: white; color: black; font-size: 10pt">
          <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">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing1
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 3,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 1"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing2
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 6,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 2"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing3
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 1,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 3"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= <span style="color: blue">new</span>[] { thing1, thing2, thing3 }.</pre>
          <pre style="margin: 0px">        Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    fake.AddThing(thing1);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing2);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing3);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <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">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
This test consists of 18 lines of code.
</p>
        <p>
Using the Fixture Object pattern, I was able to cut that down to 7 lines of code,
which is a 61% improvement (however, the downside was an additional 19 lines of (reusable)
code for MyClassFixture, so the benefit can only be reaped when you have multiple
tests leveraged by the same Fixture Object. This was all covered in the <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">former
post, to which I will refer you</a>).
</p>
        <p>
With AutoFixture, we can do much better. Here's a one-off rewrite of the unit test
using AutoFixture:
</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_AutoFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fixture.Register&lt;\cf3 IMyInterface\cf0 &gt;(() =&gt; fake);\par ??\par ??    \cf4 var\cf0  things = fixture.CreateMany&lt;\cf3 Thing\cf0 &gt;().ToList();\par ??    things.ForEach(t =&gt; fake.AddThing(t));\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf3 MyClass\cf0 &gt;();\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-family: courier new; background: white; color: black; font-size: 10pt">
          <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_AutoFixture()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Fixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(()
=&gt; fake);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> things
= fixture.CreateMany&lt;<span style="color: #2b91af">Thing</span>&gt;().ToList();</pre>
          <pre style="margin: 0px">    things.ForEach(t =&gt; fake.AddThing(t));</pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this test, I <a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx">map
the concrete fake instance to the IMyInterface type</a> in the fixture object, and
then use its ability to <a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx">create
many anonymous instances with one method call</a>. Before exercising the SUT, I also
use the fixture instance as a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT
Factory</a>.
</p>
        <p>
Apart from AutoFixture (and FakeMyInterface, which is invariant for all variations,
and thus kept out of the comparison), this test stands alone, but still manages to
reduce the number of code lines to 10 lines – a 44% improvement! In my book, that's
already a significant gain in productivity and maintainability, but we can do better!
</p>
        <p>
If we need to test MyClass repeatedly in similar ways, we can move the common code
to a Fixture Object based on AutoFixture, and the test can be refactored to 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_DerivedFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddManyTo(fixture.Things);\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf3 MyClass\cf0 &gt;();\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-family: courier new; background: white; color: black; font-size: 10pt">
          <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_DerivedFixture()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClassFixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">MyClassFixture</span>();</pre>
          <pre style="margin: 0px">    fixture.AddManyTo(fixture.Things);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= </pre>
          <pre style="margin: 0px">        fixture.Things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Now we are back at 7 lines of code, which is on par with the original Fixture Object-based
test, but now MyClassFixture is reduced to 8 lines of code:
</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\cf0  : \cf4 Fixture\par ??\cf0 \{\par ??    \cf1 internal\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 Thing\cf0 &gt;();\par ??        \cf1 this\cf0 .Register&lt;\cf4 IMyInterface\cf0 &gt;(() =&gt;\par ??            \{\par ??                \cf1 var\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??                \cf1 this\cf0 .Things.ToList().ForEach(t =&gt;\par ??                    fake.AddThing(t));\par ??                \cf1 return\cf0  fake;\par ??            \});\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &lt;\cf4 Thing\cf0 &gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">MyClassFixture</span> : <span style="color: #2b91af">Fixture</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span> MyClassFixture()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <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">        <span style="color: blue">this</span>.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(()
=&gt;</pre>
          <pre style="margin: 0px">            {</pre>
          <pre style="margin: 0px">                <span style="color: blue">var</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">                <span style="color: blue">this</span>.Things.ToList().ForEach(t
=&gt;</pre>
          <pre style="margin: 0px">                    fake.AddThing(t));</pre>
          <pre style="margin: 0px">                <span style="color: blue">return</span> fake;</pre>
          <pre style="margin: 0px">            });</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <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">}</pre>
        </div>
        <p>
Notice how I've moved the IMyInterface-to-FakeMyInterface mapping to MyClassFixture.
Whenever it's asked to create a new instance of IMyInterface, MyClassFixture makes
sure to add all the Thing instances to the fake before returning it.
</p>
        <p>
Compared to the former Fixture Object of 19 lines, that's another 58% improvement.
Considering some of the APIs I encounter in my daily work, the above example is even
rather simple. The more complex and demanding your SUT's API is, the greater the gain
from using AutoFixture will be, since it's going to figure out much of the routine
stuff for you.
</p>
        <p>
With this post, I hope I have given you a taste of the power that AutoFixture provides.
It allows you to focus on specifying the behavior of your SUT, while taking care of
all the infrastructure tedium that tends to get in the way.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ef5bab91-8b41-4cc5-9e44-f6accd855d64" />
      </body>
      <title>AutoFixture As Fixture Object</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</guid>
      <link>http://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject.aspx</link>
      <pubDate>Fri, 15 May 2009 05:34:00 GMT</pubDate>
      <description>&lt;p&gt;
Dear reader, I hope you are still with me!
&lt;/p&gt;
&lt;p&gt;
After eight posts of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; feature
walkthroughs, I can't blame you for wondering why this tool might even be relevant
to you. In this post, we'll finally begin to look at how AutoFixture can help you
towards &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;
In an earlier post, I described how the &lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;Fixture
Object&lt;/a&gt; pattern can help you greatly reduce the amount of test code that you have
to write. Since AutoFixture was designed to act as a general-purpose Fixture Object,
it can help you reduce the amount of test code even further, letting you focus on &lt;a href="http://blog.ploeh.dk/2009/03/10/SpecificationDrivenDevelopment.aspx"&gt;specifying
the behavior&lt;/a&gt; of your &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In that former post, the original example was this complex test that I will repeat
in it's entirety for your benefit (or horror):
&lt;/p&gt;
&lt;!--
{\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 =&amp;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&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing1
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 3,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 1"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing2
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 6,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 2"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing3
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 1,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 3"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { thing1, thing2, thing3 }.&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing1);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing2);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing3);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This test consists of 18 lines of code.
&lt;/p&gt;
&lt;p&gt;
Using the Fixture Object pattern, I was able to cut that down to 7 lines of code,
which is a 61% improvement (however, the downside was an additional 19 lines of (reusable)
code for MyClassFixture, so the benefit can only be reaped when you have multiple
tests leveraged by the same Fixture Object. This was all covered in the &lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;former
post, to which I will refer you&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
With AutoFixture, we can do much better. Here's a one-off rewrite of the unit test
using AutoFixture:
&lt;/p&gt;
&lt;!--
{\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_AutoFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fixture.Register&amp;lt;\cf3 IMyInterface\cf0 &amp;gt;(() =&amp;gt; fake);\par ??\par ??    \cf4 var\cf0  things = fixture.CreateMany&amp;lt;\cf3 Thing\cf0 &amp;gt;().ToList();\par ??    things.ForEach(t =&amp;gt; fake.AddThing(t));\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&amp;gt; t.Number).Sum();\par ??\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 MyClass\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_AutoFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt;&amp;gt;(()
=&amp;gt; fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; things
= fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;().ToList();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; things.ForEach(t =&amp;gt; fake.AddThing(t));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this test, I &lt;a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx"&gt;map
the concrete fake instance to the IMyInterface type&lt;/a&gt; in the fixture object, and
then use its ability to &lt;a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx"&gt;create
many anonymous instances with one method call&lt;/a&gt;. Before exercising the SUT, I also
use the fixture instance as a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT
Factory&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Apart from AutoFixture (and FakeMyInterface, which is invariant for all variations,
and thus kept out of the comparison), this test stands alone, but still manages to
reduce the number of code lines to 10 lines – a 44% improvement! In my book, that's
already a significant gain in productivity and maintainability, but we can do better!
&lt;/p&gt;
&lt;p&gt;
If we need to test MyClass repeatedly in similar ways, we can move the common code
to a Fixture Object based on AutoFixture, and the test can be refactored to this:
&lt;/p&gt;
&lt;!--
{\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_DerivedFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddManyTo(fixture.Things);\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&amp;gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 MyClass\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_DerivedFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.AddManyTo(fixture.Things);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Now we are back at 7 lines of code, which is on par with the original Fixture Object-based
test, but now MyClassFixture is reduced to 8 lines of code:
&lt;/p&gt;
&lt;!--
{\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\cf0  : \cf4 Fixture\par ??\cf0 \{\par ??    \cf1 internal\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Thing\cf0 &amp;gt;();\par ??        \cf1 this\cf0 .Register&amp;lt;\cf4 IMyInterface\cf0 &amp;gt;(() =&amp;gt;\par ??            \{\par ??                \cf1 var\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??                \cf1 this\cf0 .Things.ToList().ForEach(t =&amp;gt;\par ??                    fake.AddThing(t));\par ??                \cf1 return\cf0  fake;\par ??            \});\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Thing\cf0 &amp;gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; MyClassFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt;&amp;gt;(()
=&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things.ToList().ForEach(t
=&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(t));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; fake;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;
Things { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Notice how I've moved the IMyInterface-to-FakeMyInterface mapping to MyClassFixture.
Whenever it's asked to create a new instance of IMyInterface, MyClassFixture makes
sure to add all the Thing instances to the fake before returning it.
&lt;/p&gt;
&lt;p&gt;
Compared to the former Fixture Object of 19 lines, that's another 58% improvement.
Considering some of the APIs I encounter in my daily work, the above example is even
rather simple. The more complex and demanding your SUT's API is, the greater the gain
from using AutoFixture will be, since it's going to figure out much of the routine
stuff for you.
&lt;/p&gt;
&lt;p&gt;
With this post, I hope I have given you a taste of the power that AutoFixture provides.
It allows you to focus on specifying the behavior of your SUT, while taking care of
all the infrastructure tedium that tends to get in the way.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ef5bab91-8b41-4cc5-9e44-f6accd855d64" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</comments>
      <category>AutoFixture</category>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=57eae294-ad13-49a3-b10d-2ebff5aba940</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,57eae294-ad13-49a3-b10d-2ebff5aba940.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,57eae294-ad13-49a3-b10d-2ebff5aba940.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=57eae294-ad13-49a3-b10d-2ebff5aba940</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
At May 12 2009 I'll be speaking at 7N's <em>IT Konference 2009</em> (in Danish, so
that's no spelling error). You can read the program <a href="http://www.7n.com/dk/Downloads/test2009.pdf">here</a>.
</p>
        <p>
The topic of my talk will be TDD patterns and terminology, so I'll discuss Fixtures,
Stubs, Mocks and the like. As always, <a href="http://xunitpatterns.com/">xUnit Test
Patterns</a> will form the basis of my vocabulary.
</p>
        <p>
Of the other speakers, I'm particularly looking forward to hear my good friend Martin
Gildenpfennig from <a href="http://community.ative.dk/blogs/">Ative</a> speak!
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=57eae294-ad13-49a3-b10d-2ebff5aba940" />
      </body>
      <title>Speaking at 7N IT Conference 2009</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,57eae294-ad13-49a3-b10d-2ebff5aba940.aspx</guid>
      <link>http://blog.ploeh.dk/2009/04/28/SpeakingAt7NITConference2009.aspx</link>
      <pubDate>Tue, 28 Apr 2009 21:02:32 GMT</pubDate>
      <description>&lt;p&gt;
At May 12 2009 I'll be speaking at 7N's &lt;em&gt;IT Konference 2009&lt;/em&gt; (in Danish, so
that's no spelling error). You can read the program &lt;a href="http://www.7n.com/dk/Downloads/test2009.pdf"&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The topic of my talk will be TDD patterns and terminology, so I'll discuss Fixtures,
Stubs, Mocks and the like. As always, &lt;a href="http://xunitpatterns.com/"&gt;xUnit Test
Patterns&lt;/a&gt; will form the basis of my vocabulary.
&lt;/p&gt;
&lt;p&gt;
Of the other speakers, I'm particularly looking forward to hear my good friend Martin
Gildenpfennig from &lt;a href="http://community.ative.dk/blogs/"&gt;Ative&lt;/a&gt; speak!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=57eae294-ad13-49a3-b10d-2ebff5aba940" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,57eae294-ad13-49a3-b10d-2ebff5aba940.aspx</comments>
      <category>Miscellaneous</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2104ff91-b287-4c7c-a436-75d3c7a81f7a</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2104ff91-b287-4c7c-a436-75d3c7a81f7a</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It gives me great pleasure to announce my latest project: <a href="http://autofixture.codeplex.com/">AutoFixture</a>!
</p>
        <p>
What is AutoFixture?
</p>
        <p>
In essence, AutoFixture is a library that creates <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a> for you when you write unit tests. The intention is that it should enhance
your productivity when you do Test-Driven Development – as it has done mine.
</p>
        <p>
Instead of using mental resources on creating Anonymous Variables, AutoFixture can
do it for you. By default, it uses <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a>, but you can configure it to behave differently if you wish.
</p>
        <p>
Here’s a very basic example:
</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  IntroductoryTest()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 int\cf0  expectedNumber = fixture.CreateAnonymous&lt;\cf4 int\cf0 &gt;();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf3 MyClass\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.Echo(expectedNumber);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedNumber, result, \cf6 "Echo"\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> IntroductoryTest()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Fixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedNumber
= fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.Echo(expectedNumber);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedNumber,
result, <span style="color: #a31515">"Echo"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The Fixture class is your main entry point to AutoFixture. You can use it as is, customize
it, or derive from it as you please; it makes a great base class for a <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">Fixture
Object</a>. 
</p>
        <p>
The <em>expectedNumber</em> variable may be an <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Explicit
Expectation</a>, but its value is Anonymous, so instead of coming up with a number
ourselves, we can let the CreateAnonymous&lt;T&gt; method do it for us.
</p>
        <p>
This method can create instances of most CLR types as long as they have a public constructor
(it uses Reflection), but for many primitive types (like Int32), it has specific,
customizable algorithms for creating values using Constrained Non-Determinism.
</p>
        <p>
When creating the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, we can also
use Fixture as an excellent <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT
Factory</a>. Since it will do whatever it can to create an instance of the type you
ask for, it is pretty robust if you decide to refactor the SUT’s constructor.
</p>
        <p>
The above example only hints at what AutoFixture can do. Since the example is very
simple, it may be hard to immediately understand its value, so in future posts I will
expand on specific AutoFixture features and principles.
</p>
        <p>
Getting started with AutoFixture is as simple as downloading it from <a href="http://autofixture.codeplex.com/">CodePlex</a> and
referencing the assembly in Visual Studio.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2104ff91-b287-4c7c-a436-75d3c7a81f7a" />
      </body>
      <title>Announcing: AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/22/AnnouncingAutoFixture.aspx</link>
      <pubDate>Sun, 22 Mar 2009 06:50:54 GMT</pubDate>
      <description>&lt;p&gt;
It gives me great pleasure to announce my latest project: &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;
What is AutoFixture?
&lt;/p&gt;
&lt;p&gt;
In essence, AutoFixture is a library that creates &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt; for you when you write unit tests. The intention is that it should enhance
your productivity when you do Test-Driven Development – as it has done mine.
&lt;/p&gt;
&lt;p&gt;
Instead of using mental resources on creating Anonymous Variables, AutoFixture can
do it for you. By default, it uses &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt;, but you can configure it to behave differently if you wish.
&lt;/p&gt;
&lt;p&gt;
Here’s a very basic example:
&lt;/p&gt;
&lt;!--
{\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  IntroductoryTest()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 int\cf0  expectedNumber = fixture.CreateAnonymous&amp;lt;\cf4 int\cf0 &amp;gt;();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 MyClass\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.Echo(expectedNumber);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedNumber, result, \cf6 "Echo"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; IntroductoryTest()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedNumber
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.Echo(expectedNumber);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedNumber,
result, &lt;span style="color: #a31515"&gt;"Echo"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The Fixture class is your main entry point to AutoFixture. You can use it as is, customize
it, or derive from it as you please; it makes a great base class for a &lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;Fixture
Object&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
The &lt;em&gt;expectedNumber&lt;/em&gt; variable may be an &lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Explicit
Expectation&lt;/a&gt;, but its value is Anonymous, so instead of coming up with a number
ourselves, we can let the CreateAnonymous&amp;lt;T&amp;gt; method do it for us.
&lt;/p&gt;
&lt;p&gt;
This method can create instances of most CLR types as long as they have a public constructor
(it uses Reflection), but for many primitive types (like Int32), it has specific,
customizable algorithms for creating values using Constrained Non-Determinism.
&lt;/p&gt;
&lt;p&gt;
When creating the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;, we can also
use Fixture as an excellent &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT
Factory&lt;/a&gt;. Since it will do whatever it can to create an instance of the type you
ask for, it is pretty robust if you decide to refactor the SUT’s constructor.
&lt;/p&gt;
&lt;p&gt;
The above example only hints at what AutoFixture can do. Since the example is very
simple, it may be hard to immediately understand its value, so in future posts I will
expand on specific AutoFixture features and principles.
&lt;/p&gt;
&lt;p&gt;
Getting started with AutoFixture is as simple as downloading it from &lt;a href="http://autofixture.codeplex.com/"&gt;CodePlex&lt;/a&gt; and
referencing the assembly in Visual Studio.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2104ff91-b287-4c7c-a436-75d3c7a81f7a" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</comments>
      <category>AutoFixture</category>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=a6ac8eab-1593-4a08-ae66-f32599a51c09</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=a6ac8eab-1593-4a08-ae66-f32599a51c09</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <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">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing1
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 3,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 1"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing2
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 6,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 2"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing3
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 1,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 3"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= <span style="color: blue">new</span>[] { thing1, thing2, thing3 }.</pre>
          <pre style="margin: 0px">        Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    fake.AddThing(thing1);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing2);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing3);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <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">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <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">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing1
= <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing2
= <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing3
= <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span>[]
things = <span style="color: blue">new</span>[] { thing1, thing2, thing3 };</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClassTest</span>.AddThingsToMyInterface(fake,
things);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <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">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <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">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClassFixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">MyClassFixture</span>();</pre>
          <pre style="margin: 0px">    fixture.AddAnonymousThings();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= </pre>
          <pre style="margin: 0px">        fixture.Things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateSut();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <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">    <span style="color: blue">public</span> MyClassFixture()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.Fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">        <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">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <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"> </pre>
          <pre style="margin: 0px">    <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"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: blue">void</span> AddAnonymousThings()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">int</span> many
= 3;</pre>
          <pre style="margin: 0px">        <span style="color: blue">for</span> (<span style="color: blue">int</span> i
= 0; i &lt; many; i++)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: #2b91af">Thing</span> t
= <span style="color: blue">this</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">            <span style="color: blue">this</span>.Things.Add(t);</pre>
          <pre style="margin: 0px">            <span style="color: blue">this</span>.Fake.AddThing(t);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: #2b91af">MyClass</span> CreateSut()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <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">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: #2b91af">Thing</span> CreateAnonymousThing()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Thing</span> t
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>();</pre>
          <pre style="margin: 0px">        t.Number = <span style="color: blue">this</span>.Things.Count
+ 1;</pre>
          <pre style="margin: 0px">        t.Text = <span style="color: #2b91af">Guid</span>.NewGuid().ToString();</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> t;</pre>
          <pre style="margin: 0px">    }</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>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a6ac8eab-1593-4a08-ae66-f32599a51c09" />
      </body>
      <title>Fixture Object</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx</link>
      <pubDate>Mon, 16 Mar 2009 20:13:29 GMT</pubDate>
      <description>&lt;p&gt;
(A &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/a&gt; post)
&lt;/p&gt;
&lt;p&gt;
For a simple API, setting up the &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt; may
be as simple as creating a new instance of the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;,
and possibly any &lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Expected&lt;/a&gt; or &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt;. On the other hand, for a complex API, setting up the fixture may require
quite a bit of (potentially repetitive) code.
&lt;/p&gt;
&lt;p&gt;
Since the DRY principle also applies to test code, it quickly becomes necessary to
create test-specific helper methods and other &lt;a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20Encapsulation%20Method"&gt;SUT
API Encapsulation&lt;/a&gt;, 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.
&lt;/p&gt;
&lt;p&gt;
Let's look at an example.
&lt;/p&gt;
&lt;p&gt;
Here's a unit test with a complex &lt;a href="http://xunitpatterns.com/fixture%20setup.html"&gt;Fixture
Setup&lt;/a&gt;:
&lt;/p&gt;
&lt;!--
{\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 =&amp;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&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing1
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 3,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 1"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing2
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 6,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 2"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing3
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 1,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 3"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { thing1, thing2, thing3 }.&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing1);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing2);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing3);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
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.
&lt;/p&gt;
&lt;p&gt;
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:
&lt;/p&gt;
&lt;!--
{\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 =&amp;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&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_Helpers()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing1
= &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing2
= &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing3
= &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;[]
things = &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { thing1, thing2, thing3 };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.AddThingsToMyInterface(fake,
things);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
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.
&lt;/p&gt;
&lt;p&gt;
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.
&lt;/p&gt;
&lt;!--
{\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 =&amp;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&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_FixtureObject()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.AddAnonymousThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateSut();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
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.
&lt;/p&gt;
&lt;!--
{\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 &amp;lt;\cf4 Thing\cf0 &amp;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 &amp;lt;\cf4 Thing\cf0 &amp;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 &amp;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 ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; MyClassFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt; Fake
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;
Things { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddAnonymousThings()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; many
= 3;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i
= 0; i &amp;lt; many; i++)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; t
= &lt;span style="color: blue"&gt;this&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things.Add(t);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Fake.AddThing(t);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; CreateSut()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.Fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; CreateAnonymousThing()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; t
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Number = &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things.Count
+ 1;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Text = &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid().ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; t;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The CreateAnonymousThing method uses &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt; to create unique Thing instances. The AddAnonymousThings method
uses &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;3 as
an equivalence of many&lt;/a&gt;, and the CreateSut method acts as a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT
Factory&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
This is both more reusable and more expressive than a collection of disjointed static
helper methods.
&lt;/p&gt;
&lt;p&gt;
Whenever I begin to feel that setting up a Test Fixture is becoming too cumbersome,
Fixture Object is the first pattern I consider.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a6ac8eab-1593-4a08-ae66-f32599a51c09" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=b1e14192-4a42-4094-aae1-f70478b60c32</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,b1e14192-4a42-4094-aae1-f70478b60c32.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,b1e14192-4a42-4094-aae1-f70478b60c32.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=b1e14192-4a42-4094-aae1-f70478b60c32</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the last couple of years, there’s been a lot of debate in the community on the
philosophy behind TDD and where to put the emphasis – even to the point of debating
whether the acronym stands for Test-Driven <em>Development</em> or Test-Driven <em>Design</em>.
</p>
        <p>
Other people don’t like the emphasis on <em>tests</em>, since that makes TDD sound
like a Testing discipline, and not a Development discipline. Instead, they prefer
terms like <em>Example-Driven Design/Development</em> (EDD) or even <em>Design by
Example</em> (DbE).
</p>
        <p>
This view seems to me to be particularly prevalent in Microsoft, where there’s a rather
sharp distinction between developers and testers (job titles to the contrary) – I
guess that’s one of the reasons why <a href="http://www.codeplex.com/xunit">xUnit.net</a> (a
project initiated by Microsoft employees) uses the attribute <em>Fact</em> instead
of <em>Test</em> or <em>TestMethod</em>.
</p>
        <p>
For people used to SCRUM or other agile methodologies, this distinction is more blurred,
and they also seem to accept the <em>T</em> in TDD more willingly.
</p>
        <p>
However, the adherents of EDD claim that the mere presence of the word <em>test</em> make
some developers block any further input and stop listening. They may be right in that.
</p>
        <p>
They also claim that the <em>tests</em> in TDD/EDD are nothing more than accidental
artifacts of the development <em>process</em>, and hence argue that we shouldn’t call
them <em>tests</em> at all. However, if that’s true, <a href="http://ayende.com/Blog/archive/2007/10/19/Test-Once-The-other-side-of-continuous-integration.aspx">this
little story</a> related by <a href="http://ayende.com/Blog/Default.aspx">Ayende</a> must
be an example of EDD in its purest form :)
</p>
        <p>
To me, the tests are <em>also</em> important. Since 2003 I’ve been practicing TDD,
and while I love how it helps me arrive at better design, I also savor the safety
net that my suite of tests gives me. The tests that I write during TDD define the <em>behavior</em> of
the software. In many cases, I’d even claim that such a regression test suite is more
valuable than a Quality Assurance (QA) regression test suite – after all, a QA suite
may catch some edge cases, but they don’t focus on the intended <em>behavior</em> of
the system, but often more on how to break it - but I digress…
</p>
        <p>
My recent posts on <a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx">Executable
Specification</a> and <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a> help explain my current stance in this debate: In my opinion,
EDD fails to establish a relationship by not providing <a href="http://xunitpatterns.com/Derived%20Value.html">Derived
Values</a>. After all, what does a test like the following specify?
</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  InvertWillReverseText_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(\cf6 "heolp"\cf0 , result, \cf6 "Invert"\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> InvertWillReverseText_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(<span style="color: #a31515">"ploeh"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"heolp"</span>,
result, <span style="color: #a31515">"Invert"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
How would you implement the Invert method? Here’s one possible implementation:
</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;\red163\green21\blue21;}??\fs20 \cf1 return\cf0  \cf4 "heolp"\cf0 ;}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">return</span>
            <span style="color: #a31515">"heolp"</span>;</pre>
        </div>
        <p>
Obviously, you could now write a new test that gives a <em>second</em> example of
input and outcome and force me to write a more sophisticated algorithm. However, with
only two examples, I might still be tempted to write a switch statement with some
hard-coded return values until you’ve written so many ‘examples’ that you’ve coerced
me into writing the more general (and correct) algorithm.
</p>
        <p>
Such an approach I find inefficient.
</p>
        <p>
Instead, by using Constrained Non-Determinism to force myself to define Derived Values,
each test fully <em>specifies</em> the desired behavior. It doesn’t provide <em>examples</em>.
It provides the <em>specification</em>, and instead of having to write several similar
examples to coerce a general algorithm to emerge, I can usually nail it in a single
test.
</p>
        <p>
This approach could be styled Specification-Driven Development, and that’s how I’ve
been writing code for the last year or so.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b1e14192-4a42-4094-aae1-f70478b60c32" />
      </body>
      <title>Specification-Driven Development</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,b1e14192-4a42-4094-aae1-f70478b60c32.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/10/SpecificationDrivenDevelopment.aspx</link>
      <pubDate>Tue, 10 Mar 2009 21:04:38 GMT</pubDate>
      <description>&lt;p&gt;
In the last couple of years, there’s been a lot of debate in the community on the
philosophy behind TDD and where to put the emphasis – even to the point of debating
whether the acronym stands for Test-Driven &lt;em&gt;Development&lt;/em&gt; or Test-Driven &lt;em&gt;Design&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
Other people don’t like the emphasis on &lt;em&gt;tests&lt;/em&gt;, since that makes TDD sound
like a Testing discipline, and not a Development discipline. Instead, they prefer
terms like &lt;em&gt;Example-Driven Design/Development&lt;/em&gt; (EDD) or even &lt;em&gt;Design by
Example&lt;/em&gt; (DbE).
&lt;/p&gt;
&lt;p&gt;
This view seems to me to be particularly prevalent in Microsoft, where there’s a rather
sharp distinction between developers and testers (job titles to the contrary) – I
guess that’s one of the reasons why &lt;a href="http://www.codeplex.com/xunit"&gt;xUnit.net&lt;/a&gt; (a
project initiated by Microsoft employees) uses the attribute &lt;em&gt;Fact&lt;/em&gt; instead
of &lt;em&gt;Test&lt;/em&gt; or &lt;em&gt;TestMethod&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
For people used to SCRUM or other agile methodologies, this distinction is more blurred,
and they also seem to accept the &lt;em&gt;T&lt;/em&gt; in TDD more willingly.
&lt;/p&gt;
&lt;p&gt;
However, the adherents of EDD claim that the mere presence of the word &lt;em&gt;test&lt;/em&gt; make
some developers block any further input and stop listening. They may be right in that.
&lt;/p&gt;
&lt;p&gt;
They also claim that the &lt;em&gt;tests&lt;/em&gt; in TDD/EDD are nothing more than accidental
artifacts of the development &lt;em&gt;process&lt;/em&gt;, and hence argue that we shouldn’t call
them &lt;em&gt;tests&lt;/em&gt; at all. However, if that’s true, &lt;a href="http://ayende.com/Blog/archive/2007/10/19/Test-Once-The-other-side-of-continuous-integration.aspx"&gt;this
little story&lt;/a&gt; related by &lt;a href="http://ayende.com/Blog/Default.aspx"&gt;Ayende&lt;/a&gt; must
be an example of EDD in its purest form :)
&lt;/p&gt;
&lt;p&gt;
To me, the tests are &lt;em&gt;also&lt;/em&gt; important. Since 2003 I’ve been practicing TDD,
and while I love how it helps me arrive at better design, I also savor the safety
net that my suite of tests gives me. The tests that I write during TDD define the &lt;em&gt;behavior&lt;/em&gt; of
the software. In many cases, I’d even claim that such a regression test suite is more
valuable than a Quality Assurance (QA) regression test suite – after all, a QA suite
may catch some edge cases, but they don’t focus on the intended &lt;em&gt;behavior&lt;/em&gt; of
the system, but often more on how to break it - but I digress…
&lt;/p&gt;
&lt;p&gt;
My recent posts on &lt;a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx"&gt;Executable
Specification&lt;/a&gt; and &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt; help explain my current stance in this debate: In my opinion,
EDD fails to establish a relationship by not providing &lt;a href="http://xunitpatterns.com/Derived%20Value.html"&gt;Derived
Values&lt;/a&gt;. After all, what does a test like the following specify?
&lt;/p&gt;
&lt;!--
{\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  InvertWillReverseText_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(\cf6 "heolp"\cf0 , result, \cf6 "Invert"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(&lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;"heolp"&lt;/span&gt;,
result, &lt;span style="color: #a31515"&gt;"Invert"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
How would you implement the Invert method? Here’s one possible implementation:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \cf1 return\cf0  \cf4 "heolp"\cf0 ;}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #a31515"&gt;"heolp"&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Obviously, you could now write a new test that gives a &lt;em&gt;second&lt;/em&gt; example of
input and outcome and force me to write a more sophisticated algorithm. However, with
only two examples, I might still be tempted to write a switch statement with some
hard-coded return values until you’ve written so many ‘examples’ that you’ve coerced
me into writing the more general (and correct) algorithm.
&lt;/p&gt;
&lt;p&gt;
Such an approach I find inefficient.
&lt;/p&gt;
&lt;p&gt;
Instead, by using Constrained Non-Determinism to force myself to define Derived Values,
each test fully &lt;em&gt;specifies&lt;/em&gt; the desired behavior. It doesn’t provide &lt;em&gt;examples&lt;/em&gt;.
It provides the &lt;em&gt;specification&lt;/em&gt;, and instead of having to write several similar
examples to coerce a general algorithm to emerge, I can usually nail it in a single
test.
&lt;/p&gt;
&lt;p&gt;
This approach could be styled Specification-Driven Development, and that’s how I’ve
been writing code for the last year or so.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b1e14192-4a42-4094-aae1-f70478b60c32" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,b1e14192-4a42-4094-aae1-f70478b60c32.aspx</comments>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=4d2cf05c-151b-4cd2-8849-27d2290c19e0</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=4d2cf05c-151b-4cd2-8849-27d2290c19e0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This may turn out to be the most controversial of my <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a> posts so far, as it supposedly goes against conventional wisdom. However,
I have found this approach to be really powerful since I began using it about a year
ago.
</p>
        <p>
In my previous post, I explained how <a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx">Derived
Values help ensure that tests act as Executable Specification</a>. In short, a test
should clearly specify the relationship between input and outcome, as this test does:
</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  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\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> InvertWillReverseText()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> anonymousText
= <span style="color: #a31515">"ploeh"</span>;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
=</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: blue">string</span>(anonymousText.Reverse().ToArray());</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(anonymousText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
However, it is very tempting to just hardcode the expected value. Consistently using <a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;url=http%3a%2f%2fxunitpatterns.com%2fDerived%2520Value.html">Derived
Values</a> to establish the relationship between input and outcome requires discipline. 
</p>
        <p>
To help myself enforce this discipline, I use well-defined, but essentially random,
input, because when the input is random, I don't know the value at design time, and
hence, it is impossible for me to accidentally hard-code any assertions.
</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  InvertWillReverseText_Cnd()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf3 Guid\cf0 .NewGuid().ToString();\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\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> InvertWillReverseText_Cnd()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> anonymousText
= <span style="color: #2b91af">Guid</span>.NewGuid().ToString();</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
=</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: blue">string</span>(anonymousText.Reverse().ToArray());</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(anonymousText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
For strings, I prefer Guids, as the above example demonstrates. For numbers, I often
just use the sequence of natural numbers (i.e. <em>1, 2, 3, 4, 5...</em>). For booleans,
I often use an alternating sequence (i.e. <em>true, false, true, false...</em>).
</p>
        <p>
While this technique causes input to become non-deterministic, I always pick the non-deterministic
value-generating algorithm in such a way that it creates 'nice' values; I call this
principle <strong>Constrained Non-Determinism</strong>. Values are carefully generated
to stay far away from any boundary conditions that may cause the <a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;url=http%3a%2f%2fxunitpatterns.com%2fSUT.html">SUT</a> to
behave differently in each test run.
</p>
        <p>
Conventional unit testing wisdom dictates that unit tests should be deterministic,
so how can I possibly endorse this technique?
</p>
        <p>
To understand this, it's important to know <em>why</em> the rule about deterministic
unit tests exist. It exists because we want to be certain that each time we execute
a test suite, we verify the exact same behavior as we did the last time (given that
no tests changed). Since we also use test suites as regression tests, it's important
that we can be confident that each and every test run verifies the exact same <em>specification</em>.
</p>
        <p>
Constrained Non-Determinism doesn't invalidate that goal, because the algorithm that
generates the values must be carefully picked to always create values that stay within
the input's <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence
Class</a>.
</p>
        <p>
In a surprisingly large set of APIs, strings, for example, are treated as opaque values
that don't influence behavior in themselves. Many enterprise applications mostly store
and read data from persistent data stores, and the value of a string in itself is
often inconsequential from the point of view of the code's execution path. Data stores
may have constraints on the length of strings, so Constrained Non-Determinism dictates
that you should pick the generating algorithm so that the string length always stays
within (or exceeds, if that's what you want to test) the constraint. Guid.ToString
always returns a string with the length of 36, which is fine for a large number of
scenarios.
</p>
        <p>
Note that Constrained Non-Determinism is only relevant for <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a>. For input where the value holds a particular meaning in the context
of the SUT, you will still need to hand-pick values as always. E.g. if the input is
expected to be an XML string conforming to a particular schema, a Guid string makes
no sense.
</p>
        <p>
A secondary benefit of Constrained Non-Determinism is that you don't have to pause
to come up with values for Anonymous Variables when you are writing the test.
</p>
        <p>
While this advice may be controversial, I can only recommend it - I've been using
this technique for about a year now, and have only become more fond of it as I have
gained more experience with it.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4d2cf05c-151b-4cd2-8849-27d2290c19e0" />
      </body>
      <title>Constrained Non-Determinism</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx</link>
      <pubDate>Thu, 05 Mar 2009 20:23:05 GMT</pubDate>
      <description>&lt;p&gt;
This may turn out to be the most controversial of my &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt; posts so far, as it supposedly goes against conventional wisdom. However,
I have found this approach to be really powerful since I began using it about a year
ago.
&lt;/p&gt;
&lt;p&gt;
In my previous post, I explained how &lt;a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx"&gt;Derived
Values help ensure that tests act as Executable Specification&lt;/a&gt;. In short, a test
should clearly specify the relationship between input and outcome, as this test does:
&lt;/p&gt;
&lt;!--
{\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  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; anonymousText
= &lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(anonymousText.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(anonymousText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
However, it is very tempting to just hardcode the expected value. Consistently using &lt;a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;amp;url=http%3a%2f%2fxunitpatterns.com%2fDerived%2520Value.html"&gt;Derived
Values&lt;/a&gt; to establish the relationship between input and outcome requires discipline. 
&lt;/p&gt;
&lt;p&gt;
To help myself enforce this discipline, I use well-defined, but essentially random,
input, because when the input is random, I don't know the value at design time, and
hence, it is impossible for me to accidentally hard-code any assertions.
&lt;/p&gt;
&lt;!--
{\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  InvertWillReverseText_Cnd()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf3 Guid\cf0 .NewGuid().ToString();\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText_Cnd()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; anonymousText
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid().ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(anonymousText.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(anonymousText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
For strings, I prefer Guids, as the above example demonstrates. For numbers, I often
just use the sequence of natural numbers (i.e. &lt;em&gt;1, 2, 3, 4, 5...&lt;/em&gt;). For booleans,
I often use an alternating sequence (i.e. &lt;em&gt;true, false, true, false...&lt;/em&gt;).
&lt;/p&gt;
&lt;p&gt;
While this technique causes input to become non-deterministic, I always pick the non-deterministic
value-generating algorithm in such a way that it creates 'nice' values; I call this
principle &lt;strong&gt;Constrained Non-Determinism&lt;/strong&gt;. Values are carefully generated
to stay far away from any boundary conditions that may cause the &lt;a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;amp;url=http%3a%2f%2fxunitpatterns.com%2fSUT.html"&gt;SUT&lt;/a&gt; to
behave differently in each test run.
&lt;/p&gt;
&lt;p&gt;
Conventional unit testing wisdom dictates that unit tests should be deterministic,
so how can I possibly endorse this technique?
&lt;/p&gt;
&lt;p&gt;
To understand this, it's important to know &lt;em&gt;why&lt;/em&gt; the rule about deterministic
unit tests exist. It exists because we want to be certain that each time we execute
a test suite, we verify the exact same behavior as we did the last time (given that
no tests changed). Since we also use test suites as regression tests, it's important
that we can be confident that each and every test run verifies the exact same &lt;em&gt;specification&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
Constrained Non-Determinism doesn't invalidate that goal, because the algorithm that
generates the values must be carefully picked to always create values that stay within
the input's &lt;a href="http://xunitpatterns.com/equivalence%20class.html"&gt;Equivalence
Class&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In a surprisingly large set of APIs, strings, for example, are treated as opaque values
that don't influence behavior in themselves. Many enterprise applications mostly store
and read data from persistent data stores, and the value of a string in itself is
often inconsequential from the point of view of the code's execution path. Data stores
may have constraints on the length of strings, so Constrained Non-Determinism dictates
that you should pick the generating algorithm so that the string length always stays
within (or exceeds, if that's what you want to test) the constraint. Guid.ToString
always returns a string with the length of 36, which is fine for a large number of
scenarios.
&lt;/p&gt;
&lt;p&gt;
Note that Constrained Non-Determinism is only relevant for &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt;. For input where the value holds a particular meaning in the context
of the SUT, you will still need to hand-pick values as always. E.g. if the input is
expected to be an XML string conforming to a particular schema, a Guid string makes
no sense.
&lt;/p&gt;
&lt;p&gt;
A secondary benefit of Constrained Non-Determinism is that you don't have to pause
to come up with values for Anonymous Variables when you are writing the test.
&lt;/p&gt;
&lt;p&gt;
While this advice may be controversial, I can only recommend it - I've been using
this technique for about a year now, and have only become more fond of it as I have
gained more experience with it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4d2cf05c-151b-4cd2-8849-27d2290c19e0" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=bab36bea-feb3-436b-a2e2-2860324b704c</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=bab36bea-feb3-436b-a2e2-2860324b704c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a> post, I’d like to take a detour around the concept of tests as <a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Executable%20Specification">Executable
Specification</a>.
</p>
        <p>
An important aspect of test maintainability is readability. Tests should act both
as Executable Specification as well as <a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Documentation">documentation</a>,
which puts a lot of responsibility on the test.
</p>
        <p>
One facet of test readability is to make the relationship between the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>,
the <a href="http://xunitpatterns.com/SUT.html">SUT</a> and the verification as easy
to understand as possible. In other words, it should be clear to the <a href="http://xunitpatterns.com/test%20reader.html">Test
Reader</a> what is being asserted, and why.
</p>
        <p>
Consider a test like this one:
</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  InvertWillReverseText_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(\cf6 "heolp"\cf0 , result, \cf6 "DoWork"\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> InvertWillReverseText_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(<span style="color: #a31515">"ploeh"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"heolp"</span>,
result, <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Since this test is so simple, I expect that you can easily figure out that it implies
that the Invert method should simply reverse its input argument, but one of the reasons
this seems to be evident is because of the proximity of the two strings, as well as
the test’s name.
</p>
        <p>
In a test of a more complex API, this may not be quite as evident.
</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  DoItWillReturnCorrectResult_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.DoIt(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(42, result, \cf6 "DoIt"\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> DoItWillReturnCorrectResult_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.DoIt(<span style="color: #a31515">"ploeh"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(42,
result, <span style="color: #a31515">"DoIt"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this test, there's no apparent relationship between the input (<em>ploeh</em>)
and the output (<em>42</em>). Whatever the algorithm is behind the DoIt method, it's
completely opaque to the Test Reader, and the test fails in its role as specification
and documentation.
</p>
        <p>
Returning to the first example, it would be better if the relationship between input
and output was explicitly described:
</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  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\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> InvertWillReverseText()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> anonymousText
= <span style="color: #a31515">"ploeh"</span>;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
=</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: blue">string</span>(anonymousText.Reverse().ToArray());</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(anonymousText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this case, the input and expected outcome are clearly related, and we call the
expectedResult variable a <a href="http://xunitpatterns.com/Derived%20Value.html">Derived
Value</a>, since we explicitly derive the expected result from the input.
</p>
        <p>
Note that I’m not asking you to re-implement the whole algorithm in the test, but
only to establish a relationship. One of the main rules of thumb of unit testing is
that a test should never contain conditional branches, so there must be at least one
test case per path though the SUT.
</p>
        <p>
In the example, the Invert method actually looks like this:
</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;}??\fs20 \cf1 public\cf0  \cf1 string\cf0  Invert(\cf1 string\cf0  message)\par ??\{\par ??    \cf1 double\cf0  d;\par ??    \cf1 if\cf0  (\cf1 double\cf0 .TryParse(message, \cf1 out\cf0  d))\par ??    \{\par ??        \cf1 return\cf0  (1d / d).ToString();\par ??    \}\par ??\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf1 string\cf0 (message.Reverse().ToArray());\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">string</span> Invert(<span style="color: blue">string</span> message)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">double</span> d;</pre>
          <pre style="margin: 0px">    <span style="color: blue">if</span> (<span style="color: blue">double</span>.TryParse(message, <span style="color: blue">out</span> d))</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> (1d
/ d).ToString();</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">return</span><span style="color: blue">new</span><span style="color: blue">string</span>(message.Reverse().ToArray());</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Note that the above test only reproduces that part of the algorithm that corresponds
to the <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence Class</a> defined
by the input, whereas the branch that is triggered by a number string can be tested
by another test case that doesn’t specify string reversion.
</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  InvertWillInvertNumber()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 double\cf0  anonymousNumber = 10;\par ??    \cf4 string\cf0  numberText = anonymousNumber.ToString();\par ??    \cf4 string\cf0  expectedResult = \par ??        (1d / anonymousNumber).ToString();\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(numberText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\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> InvertWillInvertNumber()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">double</span> anonymousNumber
= 10;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> numberText
= anonymousNumber.ToString();</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
= </pre>
          <pre style="margin: 0px">        (1d / anonymousNumber).ToString();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(numberText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this way, we can break down the test cases to individual Executable Specifications
that define the expected behavior for each Equivalence Class.
</p>
        <p>
While such tests more clearly provide both specification and documentation, it requires
discipline to write tests in this way. Particularly when the algorithm is so simple
as is the case here, it's very tempting to just hard-code the values directly into
the assertion.
</p>
        <p>
In a future post, I’ll explain <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">how
we can force ourselves to do the right thing per default</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c" />
      </body>
      <title>Derived Values Ensure Executable Specification</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx</link>
      <pubDate>Tue, 03 Mar 2009 20:01:29 GMT</pubDate>
      <description>&lt;p&gt;
In this &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt; post, I’d like to take a detour around the concept of tests as &lt;a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Executable%20Specification"&gt;Executable
Specification&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
An important aspect of test maintainability is readability. Tests should act both
as Executable Specification as well as &lt;a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Documentation"&gt;documentation&lt;/a&gt;,
which puts a lot of responsibility on the test.
&lt;/p&gt;
&lt;p&gt;
One facet of test readability is to make the relationship between the &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;,
the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; and the verification as easy
to understand as possible. In other words, it should be clear to the &lt;a href="http://xunitpatterns.com/test%20reader.html"&gt;Test
Reader&lt;/a&gt; what is being asserted, and why.
&lt;/p&gt;
&lt;p&gt;
Consider a test like this one:
&lt;/p&gt;
&lt;!--
{\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  InvertWillReverseText_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(\cf6 "heolp"\cf0 , result, \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(&lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;"heolp"&lt;/span&gt;,
result, &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Since this test is so simple, I expect that you can easily figure out that it implies
that the Invert method should simply reverse its input argument, but one of the reasons
this seems to be evident is because of the proximity of the two strings, as well as
the test’s name.
&lt;/p&gt;
&lt;p&gt;
In a test of a more complex API, this may not be quite as evident.
&lt;/p&gt;
&lt;!--
{\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  DoItWillReturnCorrectResult_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.DoIt(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(42, result, \cf6 "DoIt"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DoItWillReturnCorrectResult_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.DoIt(&lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(42,
result, &lt;span style="color: #a31515"&gt;"DoIt"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this test, there's no apparent relationship between the input (&lt;em&gt;ploeh&lt;/em&gt;)
and the output (&lt;em&gt;42&lt;/em&gt;). Whatever the algorithm is behind the DoIt method, it's
completely opaque to the Test Reader, and the test fails in its role as specification
and documentation.
&lt;/p&gt;
&lt;p&gt;
Returning to the first example, it would be better if the relationship between input
and output was explicitly described:
&lt;/p&gt;
&lt;!--
{\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  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; anonymousText
= &lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(anonymousText.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(anonymousText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this case, the input and expected outcome are clearly related, and we call the
expectedResult variable a &lt;a href="http://xunitpatterns.com/Derived%20Value.html"&gt;Derived
Value&lt;/a&gt;, since we explicitly derive the expected result from the input.
&lt;/p&gt;
&lt;p&gt;
Note that I’m not asking you to re-implement the whole algorithm in the test, but
only to establish a relationship. One of the main rules of thumb of unit testing is
that a test should never contain conditional branches, so there must be at least one
test case per path though the SUT.
&lt;/p&gt;
&lt;p&gt;
In the example, the Invert method actually looks like this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 string\cf0  Invert(\cf1 string\cf0  message)\par ??\{\par ??    \cf1 double\cf0  d;\par ??    \cf1 if\cf0  (\cf1 double\cf0 .TryParse(message, \cf1 out\cf0  d))\par ??    \{\par ??        \cf1 return\cf0  (1d / d).ToString();\par ??    \}\par ??\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf1 string\cf0 (message.Reverse().ToArray());\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Invert(&lt;span style="color: blue"&gt;string&lt;/span&gt; message)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;double&lt;/span&gt; d;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (&lt;span style="color: blue"&gt;double&lt;/span&gt;.TryParse(message, &lt;span style="color: blue"&gt;out&lt;/span&gt; d))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; (1d
/ d).ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(message.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Note that the above test only reproduces that part of the algorithm that corresponds
to the &lt;a href="http://xunitpatterns.com/equivalence%20class.html"&gt;Equivalence Class&lt;/a&gt; defined
by the input, whereas the branch that is triggered by a number string can be tested
by another test case that doesn’t specify string reversion.
&lt;/p&gt;
&lt;!--
{\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  InvertWillInvertNumber()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 double\cf0  anonymousNumber = 10;\par ??    \cf4 string\cf0  numberText = anonymousNumber.ToString();\par ??    \cf4 string\cf0  expectedResult = \par ??        (1d / anonymousNumber).ToString();\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(numberText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillInvertNumber()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;double&lt;/span&gt; anonymousNumber
= 10;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; numberText
= anonymousNumber.ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (1d / anonymousNumber).ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(numberText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this way, we can break down the test cases to individual Executable Specifications
that define the expected behavior for each Equivalence Class.
&lt;/p&gt;
&lt;p&gt;
While such tests more clearly provide both specification and documentation, it requires
discipline to write tests in this way. Particularly when the algorithm is so simple
as is the case here, it's very tempting to just hard-code the values directly into
the assertion.
&lt;/p&gt;
&lt;p&gt;
In a future post, I’ll explain &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;how
we can force ourselves to do the right thing per default&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ec9010e9-3496-47a6-bed1-141becd04a13</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ec9010e9-3496-47a6-bed1-141becd04a13</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a> series, I focus on establishing a set of good habits that can potentially
make you more productive while writing tests TDD style. While being able to quickly
write good tests is important, this is not the only quality on which you should focus.
</p>
        <p>
Maintainability, not only of your production code, but also of your test code, is
important, and the DRY principle is just as applicable here.
</p>
        <p>
Consider a test 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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingConstructorToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \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> SomeTestUsingConstructorToCreateSut()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Such a test represents an anti-pattern you can easily fall victim to. The main item
of interest here is that I create the <a href="http://xunitpatterns.com/SUT.html">SUT</a> using
its constructor. You could say that I have hard-coded this particular constructor
usage into my test.
</p>
        <p>
This is not a problem if there's only one test of MyClass, but once you have many,
this starts to become a drag on your ability to refactor your code.
</p>
        <p>
Imagine that you want to change the constructor of MyClass from the default constructor
to one that takes a dependency, like this:
</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 public\cf0  MyClass(\cf4 IMyInterface\cf0  dependency)}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> MyClass(<span style="color: #2b91af">IMyInterface</span> dependency)</pre>
        </div>
        <p>
If you have many (in this case, not <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">three</a>,
but dozens) tests using the default constructor, this simple change will force you
to visit all these tests and modify them to be able to compile again.
</p>
        <p>
If, instead, we use a factory to create the SUT in each test, there's a single place
where we can go and update the creation logic.
</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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingFactoryToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf3 MyClassFactory\cf0 .Create();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \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> SomeTestUsingFactoryToCreateSut()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: #2b91af">MyClassFactory</span>.Create();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The MyClassFactory class is a test-specific helper class (more formally, it's part
of our <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation">SUT
API Encapsulation</a>) that is part of the unit test project. Using this factory,
we only need to modify the Create method to implement the constructor change.
</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 static\cf0  \cf4 MyClass\cf0  Create()\par ??\{\par ??    \cf4 IMyInterface\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (fake);\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">static</span>
            <span style="color: #2b91af">MyClass</span> Create()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    <span style="color: blue">return</span><span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(fake);</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Instead of having to modify many individual tests to support the signature change
of the constructor, there's now one central place where we can go and do that. This
pattern supports refactoring much better, so consider making this a habit of yours.
</p>
        <p>
One exception to this rule concerns tests that explicitly deal with the constructor,
such as this one:
</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;}??\fs20 [\cf3 ExpectedException\cf0 (\cf4 typeof\cf0 (\cf3 ArgumentNullException\cf0 ))]\par ??[\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  CreateWithNullMyInterfaceWillThrow()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 IMyInterface\cf0  nullMyInterface = \cf4 null\cf0 ;\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 new\cf0  \cf3 MyClass\cf0 (nullMyInterface);\par ??    \cf5 // Verify outcome (expected exception)\par ??\cf0     \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">ExpectedException</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">ArgumentNullException</span>))]</pre>
          <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> CreateWithNullMyInterfaceWillThrow()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> nullMyInterface
= <span style="color: blue">null</span>;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(nullMyInterface);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome (expected exception)</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In a case like this, where you explicitly want to deal with the constructor in an
anomalous way, I consider it reasonable to deviate from the rule of using a factory
to create the SUT. Although this may result in a need to fix the SUT creation logic
in more than one place, instead of only in the factory itself, it's likely to be constrained
to a few places instead of dozens or more, since normally, you will only have a handful
of these explicit constructor tests.
</p>
        <p>
Compared to my Zero-Friction TDD tips and tricks, this particular advice has the potential
to marginally slow you down. However, this investments pays off when you want to refactor
your SUT's constructor, and remember that you can always just write the <a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx">call
to the factory and move on without implementing it right away</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ec9010e9-3496-47a6-bed1-141becd04a13" />
      </body>
      <title>SUT Factory</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</guid>
      <link>http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx</link>
      <pubDate>Fri, 13 Feb 2009 07:56:21 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt; series, I focus on establishing a set of good habits that can potentially
make you more productive while writing tests TDD style. While being able to quickly
write good tests is important, this is not the only quality on which you should focus.
&lt;/p&gt;
&lt;p&gt;
Maintainability, not only of your production code, but also of your test code, is
important, and the DRY principle is just as applicable here.
&lt;/p&gt;
&lt;p&gt;
Consider a test like this:
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingConstructorToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; SomeTestUsingConstructorToCreateSut()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Such a test represents an anti-pattern you can easily fall victim to. The main item
of interest here is that I create the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; using
its constructor. You could say that I have hard-coded this particular constructor
usage into my test.
&lt;/p&gt;
&lt;p&gt;
This is not a problem if there's only one test of MyClass, but once you have many,
this starts to become a drag on your ability to refactor your code.
&lt;/p&gt;
&lt;p&gt;
Imagine that you want to change the constructor of MyClass from the default constructor
to one that takes a dependency, like this:
&lt;/p&gt;
&lt;!--
{\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 public\cf0  MyClass(\cf4 IMyInterface\cf0  dependency)}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; MyClass(&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; dependency)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If you have many (in this case, not &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;three&lt;/a&gt;,
but dozens) tests using the default constructor, this simple change will force you
to visit all these tests and modify them to be able to compile again.
&lt;/p&gt;
&lt;p&gt;
If, instead, we use a factory to create the SUT in each test, there's a single place
where we can go and update the creation logic.
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingFactoryToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf3 MyClassFactory\cf0 .Create();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; SomeTestUsingFactoryToCreateSut()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: #2b91af"&gt;MyClassFactory&lt;/span&gt;.Create();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The MyClassFactory class is a test-specific helper class (more formally, it's part
of our &lt;a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation"&gt;SUT
API Encapsulation&lt;/a&gt;) that is part of the unit test project. Using this factory,
we only need to modify the Create method to implement the constructor change.
&lt;/p&gt;
&lt;!--
{\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 static\cf0  \cf4 MyClass\cf0  Create()\par ??\{\par ??    \cf4 IMyInterface\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (fake);\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; Create()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Instead of having to modify many individual tests to support the signature change
of the constructor, there's now one central place where we can go and do that. This
pattern supports refactoring much better, so consider making this a habit of yours.
&lt;/p&gt;
&lt;p&gt;
One exception to this rule concerns tests that explicitly deal with the constructor,
such as this one:
&lt;/p&gt;
&lt;!--
{\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;}??\fs20 [\cf3 ExpectedException\cf0 (\cf4 typeof\cf0 (\cf3 ArgumentNullException\cf0 ))]\par ??[\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  CreateWithNullMyInterfaceWillThrow()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 IMyInterface\cf0  nullMyInterface = \cf4 null\cf0 ;\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 new\cf0  \cf3 MyClass\cf0 (nullMyInterface);\par ??    \cf5 // Verify outcome (expected exception)\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;ExpectedException&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;))]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; CreateWithNullMyInterfaceWillThrow()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; nullMyInterface
= &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(nullMyInterface);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome (expected exception)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In a case like this, where you explicitly want to deal with the constructor in an
anomalous way, I consider it reasonable to deviate from the rule of using a factory
to create the SUT. Although this may result in a need to fix the SUT creation logic
in more than one place, instead of only in the factory itself, it's likely to be constrained
to a few places instead of dozens or more, since normally, you will only have a handful
of these explicit constructor tests.
&lt;/p&gt;
&lt;p&gt;
Compared to my Zero-Friction TDD tips and tricks, this particular advice has the potential
to marginally slow you down. However, this investments pays off when you want to refactor
your SUT's constructor, and remember that you can always just write the &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx"&gt;call
to the factory and move on without implementing it right away&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ec9010e9-3496-47a6-bed1-141becd04a13" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=b0a3b644-dd52-46c4-91cf-8261c11431b3</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=b0a3b644-dd52-46c4-91cf-8261c11431b3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my original post on <a href="http://blogs.msdn.com/ploeh/archive/2008/11/13/zero-friction-tdd.aspx">Zero-Friction
TDD</a>, I continually updated the list of posts in the series, so that there would
always be a central 'table of contents' for this topic.
</p>
        <p>
Since <a href="http://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes.aspx">I'll
lose the ability to keep editing any of my previous postings</a> on the old <a href="http://blogs.msdn.com/ploeh">ploeh
blog</a>, the present post now contains the most updated list of Zero-Friction TDD
articles:
</p>
        <ul>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx">Naming
SUT Test Variables</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/11/14/naming-direct-output-variables.aspx">Naming
Direct Output Variables</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/11/21/ignore-irrelevant-return-values.aspx">Ignore
Irrelevant Return Values</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/01/testmethod-code-snippet.aspx">testmethod
Code Snippet</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 Is Many</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/11/why-use-areequal-t.aspx">Why
Use AreEqual&lt;T&gt;?</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx">Assert
Messages Are Not Optional</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx">Use
The Generate Method Stub Smart Tag To Stay In The Zone</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2009/01/12/test-driven-properties.aspx">Test-Driven
Properties</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT Factory</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx">Derived
Values Ensure Executable Specification</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Explicit Expectations</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">Fixture Object</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject.aspx">AutoFixture
As Fixture Object</a>
          </li>
        </ul>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b0a3b644-dd52-46c4-91cf-8261c11431b3" />
      </body>
      <title>Zero-Friction TDD</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</guid>
      <link>http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx</link>
      <pubDate>Wed, 28 Jan 2009 14:46:44 GMT</pubDate>
      <description>&lt;p&gt;
In my original post on &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/13/zero-friction-tdd.aspx"&gt;Zero-Friction
TDD&lt;/a&gt;, I continually updated the list of posts in the series, so that there would
always be a central 'table of contents' for this topic.
&lt;/p&gt;
&lt;p&gt;
Since &lt;a href="http://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes.aspx"&gt;I'll
lose the ability to keep editing any of my previous postings&lt;/a&gt; on the old &lt;a href="http://blogs.msdn.com/ploeh"&gt;ploeh
blog&lt;/a&gt;, the present post now contains the most updated list of Zero-Friction TDD
articles:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx"&gt;Naming
SUT Test Variables&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/14/naming-direct-output-variables.aspx"&gt;Naming
Direct Output Variables&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/21/ignore-irrelevant-return-values.aspx"&gt;Ignore
Irrelevant Return Values&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/01/testmethod-code-snippet.aspx"&gt;testmethod
Code Snippet&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;3 Is Many&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/11/why-use-areequal-t.aspx"&gt;Why
Use AreEqual&amp;lt;T&amp;gt;?&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx"&gt;Assert
Messages Are Not Optional&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx"&gt;Use
The Generate Method Stub Smart Tag To Stay In The Zone&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/12/test-driven-properties.aspx"&gt;Test-Driven
Properties&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT Factory&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx"&gt;Derived
Values Ensure Executable Specification&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Explicit Expectations&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;Fixture Object&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject.aspx"&gt;AutoFixture
As Fixture Object&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b0a3b644-dd52-46c4-91cf-8261c11431b3" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
  </channel>
</rss>