<?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 - AutoFixture</title>
    <link>http://blog.ploeh.dk/</link>
    <description>Mark Seemann's .NET blog</description>
    <language>en-us</language>
    <copyright>Mark Seemann</copyright>
    <lastBuildDate>Wed, 25 Aug 2010 19:56:38 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=04e16504-8926-45bc-9aee-6b44d9635c5d</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,04e16504-8926-45bc-9aee-6b44d9635c5d.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,04e16504-8926-45bc-9aee-6b44d9635c5d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=04e16504-8926-45bc-9aee-6b44d9635c5d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of my Twitter followers who appears to be using <a href="http://autofixture.codeplex.com/">AutoFixture</a> recently <a href="http://twitter.com/ZeroBugBounce/status/22007039917">asked
me this</a>:
</p>
        <blockquote>
          <p>
So with the AutoMoqCustomization I feel like I should get Mocks with concrete types
too (except the sut) - why am I wrong?
</p>
        </blockquote>
        <p>
AutoFixture’s extention for <a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx">auto-mocking
with Moq</a> was never meant as a general modification of behavior. Customizations <em>extend</em> the
behavior of AutoFixture; they don’t change it. There’s a subtle difference. In any
case, the auto-mocking customization was always meant as a fallback mechanism that
would create Mocks for interfaces and abstract types because AutoFixture doesn’t know
how to deal with those.
</p>
        <p>
Apparently <a href="http://twitter.com/ZeroBugBounce">@ZeroBugBounce</a> also want
concrete classes to be issued as Mock instances, which is not quite the same thing;
AutoFixture already has a strategy for that (it’s called ConstructorInvoker).
</p>
        <p>
Nevertheless I decided to spike a little on this to see if I could get it working.
It turns out I needed to open some of the auto-mocking classes a bit for extensibility
(always a good thing), so the following doesn’t work with AutoFixture 2.0 beta 1,
but will probably work with the RTW. Also please not that I’m reporting on a spike;
I haven’t thoroughly tested all edge cases.
</p>
        <p>
That said, the first thing we need to do is to remove AutoFixture’s default ConstructorInvoker
that invokes the constructor of concrete classes. This is possible with this constructor
overload:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  Fixture(\cf2 DefaultRelays\cf0  engineParts)\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> Fixture(<span style="color: #2b91af">DefaultRelays</span> engineParts)</pre>
        </div>
        <p>
This takes as input a DefaultRelays instance, which is more or less just an IEnumerable&lt;ISpecimenBuilder&gt;
(the basic building block of AutoFixture). We need to replace that with a filter that
removes the ConstructorInvoker. Here’s a derived class that does that:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FilteringRelays\cf0  : \cf2 DefaultEngineParts\cf0 \par \{\par     \cf1 private\cf0  \cf1 readonly\cf0  \cf2 Func\cf0 &lt;\cf2 ISpecimenBuilder\cf0 , \cf1 bool\cf0 &gt; spec;\par \par     \cf1 public\cf0  FilteringRelays(\cf2 Func\cf0 &lt;\cf2 ISpecimenBuilder\cf0 , \cf1 bool\cf0 &gt; specification)\par     \{\par         \cf1 if\cf0  (specification == \cf1 null\cf0 )\par         \{\par             \cf1 throw\cf0  \cf1 new\cf0  \cf2 ArgumentNullException\cf0 (\cf3 "specification"\cf0 );\par         \}\par \par         \cf1 this\cf0 .spec = specification;\par     \}\par \par     \cf1 public\cf0  \cf1 override\cf0  \cf2 IEnumerator\cf0 &lt;\cf2 ISpecimenBuilder\cf0 &gt; GetEnumerator()\par     \{\par         \cf1 var\cf0  enumerator = \cf1 base\cf0 .GetEnumerator();\par         \cf1 while\cf0  (enumerator.MoveNext())\par         \{\par             \cf1 if\cf0  (\cf1 this\cf0 .spec(enumerator.Current))\par             \{\par                 \cf1 yield\cf0  \cf1 return\cf0  enumerator.Current;\par             \}\par         \}\par     \}\par \}\par }
-->
        <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">FilteringRelays</span> : <span style="color: #2b91af">DefaultEngineParts</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">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt;
spec;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> FilteringRelays(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt;
specification)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (specification
== <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">"specification"</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.spec
= specification;</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">override</span><span style="color: #2b91af">IEnumerator</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>&gt;
GetEnumerator()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> enumerator
= <span style="color: blue">base</span>.GetEnumerator();</pre>
          <pre style="margin: 0px">        <span style="color: blue">while</span> (enumerator.MoveNext())</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">if</span> (<span style="color: blue">this</span>.spec(enumerator.Current))</pre>
          <pre style="margin: 0px">            {</pre>
          <pre style="margin: 0px">                <span style="color: blue">yield</span><span style="color: blue">return</span> enumerator.Current;</pre>
          <pre style="margin: 0px">            }</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
DefaultEngineParts already derive from DefaultRelays, so this enables us to use the
overloaded constructor to remove the ConstructorInvoker by using these filtered relays:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red43\green145\blue175;\red0\green0\blue255;}\f0 \fs19 \cf1 Func\cf0 &lt;\cf1 ISpecimenBuilder\cf0 , \cf2 bool\cf0 &gt; concreteFilter\par     = sb =&gt; !(sb \cf2 is\cf0  \cf1 ConstructorInvoker\cf0 );\par \cf2 var\cf0  relays = \cf2 new\cf0  \cf1 FilteringRelays\cf0 (concreteFilter);\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt;
concreteFilter</pre>
          <pre style="margin: 0px">    = sb =&gt; !(sb <span style="color: blue">is</span><span style="color: #2b91af">ConstructorInvoker</span>);</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> relays
= <span style="color: blue">new</span><span style="color: #2b91af">FilteringRelays</span>(concreteFilter);</pre>
        </div>
        <p>
The second thing we need to do is to tell the AutoMoqCustomization that it should
Mock <em>all</em> types, not just interfaces and abstract classes. With the new (not
in beta 1) overload of the constructor, we can now supply a <a href="http://en.wikipedia.org/wiki/Specification_pattern">Specification</a> that
determines which types should be mocked.:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red43\green145\blue175;\red0\green0\blue255;}\f0 \fs19 \cf1 Func\cf0 &lt;\cf1 Type\cf0 , \cf2 bool\cf0 &gt; mockSpec = t =&gt; \cf2 true\cf0 ;\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: blue">bool</span>&gt;
mockSpec = t =&gt; <span style="color: blue">true</span>;</pre>
        </div>
        <p>
We can now create the Fixture like this to get auto-mocking of all types:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 var\cf0  fixture = \cf1 new\cf0  \cf2 Fixture\cf0 (relays).Customize(\par     \cf1 new\cf0  \cf2 AutoMoqCustomization\cf0 (\cf1 new\cf0  \cf2 MockRelay\cf0 (mockSpec)));\par }
-->
        <div style="font-family: consolas; 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>(relays).Customize(</pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">AutoMoqCustomization</span>(<span style="color: blue">new</span><span style="color: #2b91af">MockRelay</span>(mockSpec)));</pre>
        </div>
        <p>
With this Fixture instance, we can now create concrete classes that are mocked. Here’s
the full test that proves it:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}\f0 \fs19 [\cf1 Fact\cf0 ]\par \cf2 public\cf0  \cf2 void\cf0  CreateAnonymousMockOfConcreteType()\par \{\par     \cf3 // Fixture setup\cf0 \par     \cf1 Func\cf0 &lt;\cf1 ISpecimenBuilder\cf0 , \cf2 bool\cf0 &gt; concreteFilter\par         = sb =&gt; !(sb \cf2 is\cf0  \cf1 ConstructorInvoker\cf0 );\par     \cf2 var\cf0  relays = \cf2 new\cf0  \cf1 FilteringRelays\cf0 (concreteFilter);\par \par     \cf1 Func\cf0 &lt;\cf1 Type\cf0 , \cf2 bool\cf0 &gt; mockSpec = t =&gt; \cf2 true\cf0 ;\par \par     \cf2 var\cf0  fixture = \cf2 new\cf0  \cf1 Fixture\cf0 (relays).Customize(\par         \cf2 new\cf0  \cf1 AutoMoqCustomization\cf0 (\cf2 new\cf0  \cf1 MockRelay\cf0 (mockSpec)));\par     \cf3 // Exercise system\cf0 \par     \cf2 var\cf0  foo = fixture.CreateAnonymous&lt;\cf1 Foo\cf0 &gt;();\par     foo.DoIt();\par     \cf3 // Verify outcome\cf0 \par     \cf2 var\cf0  fooTD = \cf1 Mock\cf0 .Get(foo);\par     fooTD.Verify(f =&gt; f.DoIt());\par     \cf3 // Teardown\cf0 \par \}\par }
-->
        <div style="font-family: consolas; 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> CreateAnonymousMockOfConcreteType()</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">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt;
concreteFilter</pre>
          <pre style="margin: 0px">        = sb =&gt; !(sb <span style="color: blue">is</span><span style="color: #2b91af">ConstructorInvoker</span>);</pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> relays
= <span style="color: blue">new</span><span style="color: #2b91af">FilteringRelays</span>(concreteFilter);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: blue">bool</span>&gt;
mockSpec = t =&gt; <span style="color: blue">true</span>;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>(relays).Customize(</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: #2b91af">AutoMoqCustomization</span>(<span style="color: blue">new</span><span style="color: #2b91af">MockRelay</span>(mockSpec)));</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> foo
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Foo</span>&gt;();</pre>
          <pre style="margin: 0px">    foo.DoIt();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> fooTD
= <span style="color: #2b91af">Mock</span>.Get(foo);</pre>
          <pre style="margin: 0px">    fooTD.Verify(f =&gt; f.DoIt());</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Foo is this concrete class:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 Foo\cf0 \par \{\par     \cf1 public\cf0  \cf1 virtual\cf0  \cf1 void\cf0  DoIt()\par     \{\par     \}\par \}\par }
-->
        <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">Foo</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">virtual</span><span style="color: blue">void</span> DoIt()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Finally, a word of caution: this is a <em>spike</em>. It’s not fully tested and is
bound to fail in certain cases: at least one case is when the type to be created is
sealed. Since <a href="http://code.google.com/p/moq/">Moq</a> can’t create a Mock
of a sealed type, the above code will fail in that case. However, we can address this
issue with some more sophisticated filters and Specifications. However, I will leave
that up to the interested reader (or a later blog post).
</p>
        <p>
All in all I think this provides an excellent glimpse of the degree of extensibility
that is built into AutoFixture 2.0’s kernel.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=04e16504-8926-45bc-9aee-6b44d9635c5d" />
      </body>
      <title>Changing the behavior of AutoFixture auto-mocking with Moq</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,04e16504-8926-45bc-9aee-6b44d9635c5d.aspx</guid>
      <link>http://blog.ploeh.dk/2010/08/25/ChangingTheBehaviorOfAutoFixtureAutomockingWithMoq.aspx</link>
      <pubDate>Wed, 25 Aug 2010 19:56:38 GMT</pubDate>
      <description>&lt;p&gt;
One of my Twitter followers who appears to be using &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; recently &lt;a href="http://twitter.com/ZeroBugBounce/status/22007039917"&gt;asked
me this&lt;/a&gt;:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
So with the AutoMoqCustomization I feel like I should get Mocks with concrete types
too (except the sut) - why am I wrong?
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
AutoFixture’s extention for &lt;a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx"&gt;auto-mocking
with Moq&lt;/a&gt; was never meant as a general modification of behavior. Customizations &lt;em&gt;extend&lt;/em&gt; the
behavior of AutoFixture; they don’t change it. There’s a subtle difference. In any
case, the auto-mocking customization was always meant as a fallback mechanism that
would create Mocks for interfaces and abstract types because AutoFixture doesn’t know
how to deal with those.
&lt;/p&gt;
&lt;p&gt;
Apparently &lt;a href="http://twitter.com/ZeroBugBounce"&gt;@ZeroBugBounce&lt;/a&gt; also want
concrete classes to be issued as Mock instances, which is not quite the same thing;
AutoFixture already has a strategy for that (it’s called ConstructorInvoker).
&lt;/p&gt;
&lt;p&gt;
Nevertheless I decided to spike a little on this to see if I could get it working.
It turns out I needed to open some of the auto-mocking classes a bit for extensibility
(always a good thing), so the following doesn’t work with AutoFixture 2.0 beta 1,
but will probably work with the RTW. Also please not that I’m reporting on a spike;
I haven’t thoroughly tested all edge cases.
&lt;/p&gt;
&lt;p&gt;
That said, the first thing we need to do is to remove AutoFixture’s default ConstructorInvoker
that invokes the constructor of concrete classes. This is possible with this constructor
overload:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  Fixture(\cf2 DefaultRelays\cf0  engineParts)\par }
--&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; Fixture(&lt;span style="color: #2b91af"&gt;DefaultRelays&lt;/span&gt; engineParts)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This takes as input a DefaultRelays instance, which is more or less just an IEnumerable&amp;lt;ISpecimenBuilder&amp;gt;
(the basic building block of AutoFixture). We need to replace that with a filter that
removes the ConstructorInvoker. Here’s a derived class that does that:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FilteringRelays\cf0  : \cf2 DefaultEngineParts\cf0 \par \{\par     \cf1 private\cf0  \cf1 readonly\cf0  \cf2 Func\cf0 &amp;lt;\cf2 ISpecimenBuilder\cf0 , \cf1 bool\cf0 &amp;gt; spec;\par \par     \cf1 public\cf0  FilteringRelays(\cf2 Func\cf0 &amp;lt;\cf2 ISpecimenBuilder\cf0 , \cf1 bool\cf0 &amp;gt; specification)\par     \{\par         \cf1 if\cf0  (specification == \cf1 null\cf0 )\par         \{\par             \cf1 throw\cf0  \cf1 new\cf0  \cf2 ArgumentNullException\cf0 (\cf3 "specification"\cf0 );\par         \}\par \par         \cf1 this\cf0 .spec = specification;\par     \}\par \par     \cf1 public\cf0  \cf1 override\cf0  \cf2 IEnumerator\cf0 &amp;lt;\cf2 ISpecimenBuilder\cf0 &amp;gt; GetEnumerator()\par     \{\par         \cf1 var\cf0  enumerator = \cf1 base\cf0 .GetEnumerator();\par         \cf1 while\cf0  (enumerator.MoveNext())\par         \{\par             \cf1 if\cf0  (\cf1 this\cf0 .spec(enumerator.Current))\par             \{\par                 \cf1 yield\cf0  \cf1 return\cf0  enumerator.Current;\par             \}\par         \}\par     \}\par \}\par }
--&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;FilteringRelays&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DefaultEngineParts&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;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ISpecimenBuilder&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
spec;&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; FilteringRelays(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ISpecimenBuilder&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
specification)&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; (specification
== &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;"specification"&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;.spec
= specification;&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;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ISpecimenBuilder&lt;/span&gt;&amp;gt;
GetEnumerator()&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;var&lt;/span&gt; enumerator
= &lt;span style="color: blue"&gt;base&lt;/span&gt;.GetEnumerator();&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;while&lt;/span&gt; (enumerator.MoveNext())&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; (&lt;span style="color: blue"&gt;this&lt;/span&gt;.spec(enumerator.Current))&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;yield&lt;/span&gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; enumerator.Current;&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;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
DefaultEngineParts already derive from DefaultRelays, so this enables us to use the
overloaded constructor to remove the ConstructorInvoker by using these filtered relays:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red43\green145\blue175;\red0\green0\blue255;}\f0 \fs19 \cf1 Func\cf0 &amp;lt;\cf1 ISpecimenBuilder\cf0 , \cf2 bool\cf0 &amp;gt; concreteFilter\par     = sb =&amp;gt; !(sb \cf2 is\cf0  \cf1 ConstructorInvoker\cf0 );\par \cf2 var\cf0  relays = \cf2 new\cf0  \cf1 FilteringRelays\cf0 (concreteFilter);\par }
--&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;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ISpecimenBuilder&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
concreteFilter&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; = sb =&amp;gt; !(sb &lt;span style="color: blue"&gt;is&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConstructorInvoker&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; relays
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FilteringRelays&lt;/span&gt;(concreteFilter);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The second thing we need to do is to tell the AutoMoqCustomization that it should
Mock &lt;em&gt;all&lt;/em&gt; types, not just interfaces and abstract classes. With the new (not
in beta 1) overload of the constructor, we can now supply a &lt;a href="http://en.wikipedia.org/wiki/Specification_pattern"&gt;Specification&lt;/a&gt; that
determines which types should be mocked.:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red43\green145\blue175;\red0\green0\blue255;}\f0 \fs19 \cf1 Func\cf0 &amp;lt;\cf1 Type\cf0 , \cf2 bool\cf0 &amp;gt; mockSpec = t =&amp;gt; \cf2 true\cf0 ;\par }
--&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;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
mockSpec = t =&amp;gt; &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
We can now create the Fixture like this to get auto-mocking of all types:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 var\cf0  fixture = \cf1 new\cf0  \cf2 Fixture\cf0 (relays).Customize(\par     \cf1 new\cf0  \cf2 AutoMoqCustomization\cf0 (\cf1 new\cf0  \cf2 MockRelay\cf0 (mockSpec)));\par }
--&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; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;(relays).Customize(&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;AutoMoqCustomization&lt;/span&gt;(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MockRelay&lt;/span&gt;(mockSpec)));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
With this Fixture instance, we can now create concrete classes that are mocked. Here’s
the full test that proves it:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}\f0 \fs19 [\cf1 Fact\cf0 ]\par \cf2 public\cf0  \cf2 void\cf0  CreateAnonymousMockOfConcreteType()\par \{\par     \cf3 // Fixture setup\cf0 \par     \cf1 Func\cf0 &amp;lt;\cf1 ISpecimenBuilder\cf0 , \cf2 bool\cf0 &amp;gt; concreteFilter\par         = sb =&amp;gt; !(sb \cf2 is\cf0  \cf1 ConstructorInvoker\cf0 );\par     \cf2 var\cf0  relays = \cf2 new\cf0  \cf1 FilteringRelays\cf0 (concreteFilter);\par \par     \cf1 Func\cf0 &amp;lt;\cf1 Type\cf0 , \cf2 bool\cf0 &amp;gt; mockSpec = t =&amp;gt; \cf2 true\cf0 ;\par \par     \cf2 var\cf0  fixture = \cf2 new\cf0  \cf1 Fixture\cf0 (relays).Customize(\par         \cf2 new\cf0  \cf1 AutoMoqCustomization\cf0 (\cf2 new\cf0  \cf1 MockRelay\cf0 (mockSpec)));\par     \cf3 // Exercise system\cf0 \par     \cf2 var\cf0  foo = fixture.CreateAnonymous&amp;lt;\cf1 Foo\cf0 &amp;gt;();\par     foo.DoIt();\par     \cf3 // Verify outcome\cf0 \par     \cf2 var\cf0  fooTD = \cf1 Mock\cf0 .Get(foo);\par     fooTD.Verify(f =&amp;gt; f.DoIt());\par     \cf3 // Teardown\cf0 \par \}\par }
--&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;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; CreateAnonymousMockOfConcreteType()&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;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ISpecimenBuilder&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
concreteFilter&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = sb =&amp;gt; !(sb &lt;span style="color: blue"&gt;is&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConstructorInvoker&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; relays
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FilteringRelays&lt;/span&gt;(concreteFilter);&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;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
mockSpec = t =&amp;gt; &lt;span style="color: blue"&gt;true&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; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;(relays).Customize(&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;AutoMoqCustomization&lt;/span&gt;(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MockRelay&lt;/span&gt;(mockSpec)));&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; foo
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foo.DoIt();&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; fooTD
= &lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;.Get(foo);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fooTD.Verify(f =&amp;gt; f.DoIt());&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;
Foo is this concrete class:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 Foo\cf0 \par \{\par     \cf1 public\cf0  \cf1 virtual\cf0  \cf1 void\cf0  DoIt()\par     \{\par     \}\par \}\par }
--&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;Foo&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;virtual&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DoIt()&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;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Finally, a word of caution: this is a &lt;em&gt;spike&lt;/em&gt;. It’s not fully tested and is
bound to fail in certain cases: at least one case is when the type to be created is
sealed. Since &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; can’t create a Mock
of a sealed type, the above code will fail in that case. However, we can address this
issue with some more sophisticated filters and Specifications. However, I will leave
that up to the interested reader (or a later blog post).
&lt;/p&gt;
&lt;p&gt;
All in all I think this provides an excellent glimpse of the degree of extensibility
that is built into AutoFixture 2.0’s kernel.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=04e16504-8926-45bc-9aee-6b44d9635c5d" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,04e16504-8926-45bc-9aee-6b44d9635c5d.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <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=3b0e46a4-1566-46e8-9451-3681dac68d4d</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3b0e46a4-1566-46e8-9451-3681dac68d4d.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3b0e46a4-1566-46e8-9451-3681dac68d4d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3b0e46a4-1566-46e8-9451-3681dac68d4d</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It gives me great pleasure to announce that <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0
beta 1 is now <a href="http://autofixture.codeplex.com/releases/view/50304">available
for download</a>. Compared to version 1.1 AutoFixture 2.0 is implemented using a new
and much more open and extensible engine that enables many interesting scenarios.
</p>
        <p>
Despite the new engine and the vastly increased potential, the focus on version 2.0
has been to ensure that the current, known feature set was preserved.
</p>
        <p>
          <strong>What’s new?</strong>
        </p>
        <p>
While AutoFixture 2.0 introduces new features, this release is first and foremost
an upgrade to a completely new internal architecture, so the number of new releases
is limited. Never the less, the following new features are available in version 2.0:
</p>
        <ul>
          <li>
            <a href="http://autofixture.codeplex.com/workitem/1744">Support for enums</a>
          </li>
          <li>
Full <a href="http://autofixture.codeplex.com/workitem/4199">support of arrays</a></li>
          <li>
Optional <a href="http://autofixture.codeplex.com/workitem/1913">tracing</a></li>
          <li>
Improved extensibility 
</li>
          <li>
Optional auto-mocking with <a href="http://code.google.com/p/moq/">Moq</a> (implemented
as a separate, optional assembly) 
</li>
          <li>
Optional extensions for <a href="http://xunit.codeplex.com/">xUnit.net</a> (implemented
as a separate, optional assembly)</li>
        </ul>
        <p>
There are still <a href="http://autofixture.codeplex.com/workitem/list/basic">open
feature requests</a> for AutoFixture, so now that the new engine is in place I can
again focus on implementing some of the features that were too difficult to address
with the old engine.
</p>
        <p>
          <strong>Breaking changes</strong>
        </p>
        <p>
Version 2.0 introduces some breaking changes, although the fundamental API remains
recognizable.
</p>
        <ul>
          <li>
A lot of the original methods on Fixture are now extension methods on IFixture (a
new interface). The methods include CreateAnonymous&lt;T&gt;, CreateMany&lt;T&gt;
and many others, so you will need to include a using directive for Ploeh.AutoFixture
to compile the unit test code. 
</li>
          <li>
CreateMany&lt;T&gt; still returns IEnumerable&lt;T&gt;, but the return value is much
more consistently deferred. This means that in almost all cases you should instantly
stabilize the sequence by converting it to a List&lt;T&gt;, an array or similar. 
</li>
          <li>
Several methods are now deprecated in favor of new methods with better names.</li>
        </ul>
        <p>
A few methods were also removed, but only those that were already deprecated in version
1.1.
</p>
        <p>
          <strong>Roadmap</strong>
        </p>
        <p>
The general availability of beta 1 of AutoFixture 2.0 marks the beginning of a trial
period. If no new issues are reported within the next few weeks, a final version 2.0
will be released. If too many issues are reported, a new beta version may be necessary.
</p>
        <p>
Please <a href="http://autofixture.codeplex.com/workitem/list/basic">report any issues</a> you
find.
</p>
        <p>
After the release of the final version 2.0 my plan is currently to focus on the <a href="http://autofixture.codeplex.com/workitem/1650">Idioms
project</a>, although there are many other new features that might warrant my attention.
</p>
        <p>
Blog posts about the new features will also follow soon.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3b0e46a4-1566-46e8-9451-3681dac68d4d" />
      </body>
      <title>AutoFixture 2.0 beta 1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3b0e46a4-1566-46e8-9451-3681dac68d4d.aspx</guid>
      <link>http://blog.ploeh.dk/2010/08/09/AutoFixture20Beta1.aspx</link>
      <pubDate>Mon, 09 Aug 2010 11:32:06 GMT</pubDate>
      <description>&lt;p&gt;
It gives me great pleasure to announce that &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 2.0
beta 1 is now &lt;a href="http://autofixture.codeplex.com/releases/view/50304"&gt;available
for download&lt;/a&gt;. Compared to version 1.1 AutoFixture 2.0 is implemented using a new
and much more open and extensible engine that enables many interesting scenarios.
&lt;/p&gt;
&lt;p&gt;
Despite the new engine and the vastly increased potential, the focus on version 2.0
has been to ensure that the current, known feature set was preserved.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;What’s new?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
While AutoFixture 2.0 introduces new features, this release is first and foremost
an upgrade to a completely new internal architecture, so the number of new releases
is limited. Never the less, the following new features are available in version 2.0:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://autofixture.codeplex.com/workitem/1744"&gt;Support for enums&lt;/a&gt; 
&lt;li&gt;
Full &lt;a href="http://autofixture.codeplex.com/workitem/4199"&gt;support of arrays&lt;/a&gt; 
&lt;li&gt;
Optional &lt;a href="http://autofixture.codeplex.com/workitem/1913"&gt;tracing&lt;/a&gt; 
&lt;li&gt;
Improved extensibility 
&lt;li&gt;
Optional auto-mocking with &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; (implemented
as a separate, optional assembly) 
&lt;li&gt;
Optional extensions for &lt;a href="http://xunit.codeplex.com/"&gt;xUnit.net&lt;/a&gt; (implemented
as a separate, optional assembly)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
There are still &lt;a href="http://autofixture.codeplex.com/workitem/list/basic"&gt;open
feature requests&lt;/a&gt; for AutoFixture, so now that the new engine is in place I can
again focus on implementing some of the features that were too difficult to address
with the old engine.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Breaking changes&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Version 2.0 introduces some breaking changes, although the fundamental API remains
recognizable.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A lot of the original methods on Fixture are now extension methods on IFixture (a
new interface). The methods include CreateAnonymous&amp;lt;T&amp;gt;, CreateMany&amp;lt;T&amp;gt;
and many others, so you will need to include a using directive for Ploeh.AutoFixture
to compile the unit test code. 
&lt;li&gt;
CreateMany&amp;lt;T&amp;gt; still returns IEnumerable&amp;lt;T&amp;gt;, but the return value is much
more consistently deferred. This means that in almost all cases you should instantly
stabilize the sequence by converting it to a List&amp;lt;T&amp;gt;, an array or similar. 
&lt;li&gt;
Several methods are now deprecated in favor of new methods with better names.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
A few methods were also removed, but only those that were already deprecated in version
1.1.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Roadmap&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The general availability of beta 1 of AutoFixture 2.0 marks the beginning of a trial
period. If no new issues are reported within the next few weeks, a final version 2.0
will be released. If too many issues are reported, a new beta version may be necessary.
&lt;/p&gt;
&lt;p&gt;
Please &lt;a href="http://autofixture.codeplex.com/workitem/list/basic"&gt;report any issues&lt;/a&gt; you
find.
&lt;/p&gt;
&lt;p&gt;
After the release of the final version 2.0 my plan is currently to focus on the &lt;a href="http://autofixture.codeplex.com/workitem/1650"&gt;Idioms
project&lt;/a&gt;, although there are many other new features that might warrant my attention.
&lt;/p&gt;
&lt;p&gt;
Blog posts about the new features will also follow soon.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3b0e46a4-1566-46e8-9451-3681dac68d4d" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3b0e46a4-1566-46e8-9451-3681dac68d4d.aspx</comments>
      <category>AutoFixture</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=f64de247-b9c0-42f1-b927-ebce6dadce97</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,f64de247-b9c0-42f1-b927-ebce6dadce97.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,f64de247-b9c0-42f1-b927-ebce6dadce97.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=f64de247-b9c0-42f1-b927-ebce6dadce97</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.1 is now available on
the CodePlex site! Compared to the Release Candidate, there are no changes.
</p>
        <p>
The <a href="http://autofixture.codeplex.com/releases/view/43351">1.1 release page</a> has
more details about this particular release, but essentially this is the RC promoted
to release status.
</p>
        <p>
Release 1.1 is an interim release that addresses a few issues that appeared since
the release of version 1.0. Work continues on AutoFixture 2.0 in parallel.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f64de247-b9c0-42f1-b927-ebce6dadce97" />
      </body>
      <title>AutoFixture 1.1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,f64de247-b9c0-42f1-b927-ebce6dadce97.aspx</guid>
      <link>http://blog.ploeh.dk/2010/04/10/AutoFixture11.aspx</link>
      <pubDate>Sat, 10 Apr 2010 10:25:23 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 1.1 is now available on
the CodePlex site! Compared to the Release Candidate, there are no changes.
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/releases/view/43351"&gt;1.1 release page&lt;/a&gt; has
more details about this particular release, but essentially this is the RC promoted
to release status.
&lt;/p&gt;
&lt;p&gt;
Release 1.1 is an interim release that addresses a few issues that appeared since
the release of version 1.0. Work continues on AutoFixture 2.0 in parallel.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f64de247-b9c0-42f1-b927-ebce6dadce97" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,f64de247-b9c0-42f1-b927-ebce6dadce97.aspx</comments>
      <category>AutoFixture</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=3dcb0700-6139-4d7a-ac48-8ce3950fe42e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3dcb0700-6139-4d7a-ac48-8ce3950fe42e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3dcb0700-6139-4d7a-ac48-8ce3950fe42e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3dcb0700-6139-4d7a-ac48-8ce3950fe42e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.1 Release Candidate 1
is now available on the CodePlex site.
</p>
        <p>
Users are encouraged to evaluate this RC and submit feedback. If no bugs or issues
are reported within the next week, we will promote RC1 to version 1.1.
</p>
        <p>
The <a href="http://autofixture.codeplex.com/releases/view/42969">release page</a> has
more details about this particular release.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3dcb0700-6139-4d7a-ac48-8ce3950fe42e" />
      </body>
      <title>AutoFixture 1.1 RC1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3dcb0700-6139-4d7a-ac48-8ce3950fe42e.aspx</guid>
      <link>http://blog.ploeh.dk/2010/04/02/AutoFixture11RC1.aspx</link>
      <pubDate>Fri, 02 Apr 2010 06:44:27 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 1.1 Release Candidate 1
is now available on the CodePlex site.
&lt;/p&gt;
&lt;p&gt;
Users are encouraged to evaluate this RC and submit feedback. If no bugs or issues
are reported within the next week, we will promote RC1 to version 1.1.
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/releases/view/42969"&gt;release page&lt;/a&gt; has
more details about this particular release.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3dcb0700-6139-4d7a-ac48-8ce3950fe42e" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3dcb0700-6139-4d7a-ac48-8ce3950fe42e.aspx</comments>
      <category>AutoFixture</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=60c2b13b-fc87-413b-bf88-d66513dbeddb</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,60c2b13b-fc87-413b-bf88-d66513dbeddb.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,60c2b13b-fc87-413b-bf88-d66513dbeddb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=60c2b13b-fc87-413b-bf88-d66513dbeddb</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 is now available on
the CodePlex site! Compared to Release Candidate 2 there are no changes.
</p>
        <p>
The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38630">1.0
release page</a> has more details about this particular release, but essentially this
is RC2 promoted to release status.
</p>
        <p>
It's been almost a year since I started development on AutoFixture and I must say
that it has been an exciting and fulfilling experience! The API has evolved, but has
turned out to be surprisingly flexible, yet robust. I even had some positive surprises
along the way as it dawned on me that I could do new fancy things I hadn't originally
considered.
</p>
        <p>
If you use the Likeness feature (of which I have yet to write), you will run into <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=524901">this
bug in Visual Studio</a>. The bug is only in IntelliSense, so any code using Likeness
will compile and work just fine.
</p>
        <p>
While this release marks the end of AutoFixture's initial days, it also marks the
beginning of AutoFixture 2.0. I already have lots of plans for making it even more
extensible and powerful, as well as plans for utility libraries that integrate with,
say, Moq or Rhino Mocks. It's going to be an exciting new voyage!
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=60c2b13b-fc87-413b-bf88-d66513dbeddb" />
      </body>
      <title>AutoFixture 1.0</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,60c2b13b-fc87-413b-bf88-d66513dbeddb.aspx</guid>
      <link>http://blog.ploeh.dk/2010/01/27/AutoFixture10.aspx</link>
      <pubDate>Wed, 27 Jan 2010 22:54:58 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 1.0 is now available on
the CodePlex site! Compared to Release Candidate 2 there are no changes.
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38630"&gt;1.0
release page&lt;/a&gt; has more details about this particular release, but essentially this
is RC2 promoted to release status.
&lt;/p&gt;
&lt;p&gt;
It's been almost a year since I started development on AutoFixture and I must say
that it has been an exciting and fulfilling experience! The API has evolved, but has
turned out to be surprisingly flexible, yet robust. I even had some positive surprises
along the way as it dawned on me that I could do new fancy things I hadn't originally
considered.
&lt;/p&gt;
&lt;p&gt;
If you use the Likeness feature (of which I have yet to write), you will run into &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=524901"&gt;this
bug in Visual Studio&lt;/a&gt;. The bug is only in IntelliSense, so any code using Likeness
will compile and work just fine.
&lt;/p&gt;
&lt;p&gt;
While this release marks the end of AutoFixture's initial days, it also marks the
beginning of AutoFixture 2.0. I already have lots of plans for making it even more
extensible and powerful, as well as plans for utility libraries that integrate with,
say, Moq or Rhino Mocks. It's going to be an exciting new voyage!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=60c2b13b-fc87-413b-bf88-d66513dbeddb" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,60c2b13b-fc87-413b-bf88-d66513dbeddb.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=18f01f5d-d73e-45b0-bb2b-03890b43256b</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,18f01f5d-d73e-45b0-bb2b-03890b43256b.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,18f01f5d-d73e-45b0-bb2b-03890b43256b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=18f01f5d-d73e-45b0-bb2b-03890b43256b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 Release Candidate 2
is now available on the CodePlex site! Compared to Release Candidate 1 there are very
few changes, but the test period uncovered the need for a few extra methods on a recent
addition to the library. RC2 contains these additional methods.
</p>
        <p>
This resets the clock for the Release Candidate trial period. Key users have a chance
to veto this release until a week from now. If no-one complains within that period,
we will promote RC2 to version 1.0.
</p>
        <p>
The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39161">RC2
release page</a> has more details about this particular release.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=18f01f5d-d73e-45b0-bb2b-03890b43256b" />
      </body>
      <title>AutoFixture 1.0 RC2</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,18f01f5d-d73e-45b0-bb2b-03890b43256b.aspx</guid>
      <link>http://blog.ploeh.dk/2010/01/20/AutoFixture10RC2.aspx</link>
      <pubDate>Wed, 20 Jan 2010 22:59:39 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 1.0 Release Candidate 2
is now available on the CodePlex site! Compared to Release Candidate 1 there are very
few changes, but the test period uncovered the need for a few extra methods on a recent
addition to the library. RC2 contains these additional methods.
&lt;/p&gt;
&lt;p&gt;
This resets the clock for the Release Candidate trial period. Key users have a chance
to veto this release until a week from now. If no-one complains within that period,
we will promote RC2 to version 1.0.
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39161"&gt;RC2
release page&lt;/a&gt; has more details about this particular release.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=18f01f5d-d73e-45b0-bb2b-03890b43256b" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,18f01f5d-d73e-45b0-bb2b-03890b43256b.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=9d21d46b-ecde-4d81-bfbb-71f1a6e342a7</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,9d21d46b-ecde-4d81-bfbb-71f1a6e342a7.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,9d21d46b-ecde-4d81-bfbb-71f1a6e342a7.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=9d21d46b-ecde-4d81-bfbb-71f1a6e342a7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 Release Candidate 1
is now available on the CodePlex site! AutoFixture is now almost ten months old and
has seen extensive use internally in <a href="http://www.safewhere.net/">Safewhere</a> during
most of this time. It has proven to be very stable, expressive and generally a delight
to use.
</p>
        <p>
If all goes well, the Release Candidate period will be short. Key users have a chance
to veto the this version, but if no-one complains within a week from now, we will
promote RC1 to version 1.0.
</p>
        <p>
The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38665">RC1
release page</a> has more detail about this particular release.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9d21d46b-ecde-4d81-bfbb-71f1a6e342a7" />
      </body>
      <title>AutoFixture 1.0 RC1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,9d21d46b-ecde-4d81-bfbb-71f1a6e342a7.aspx</guid>
      <link>http://blog.ploeh.dk/2010/01/13/AutoFixture10RC1.aspx</link>
      <pubDate>Wed, 13 Jan 2010 22:48:13 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 1.0 Release Candidate 1
is now available on the CodePlex site! AutoFixture is now almost ten months old and
has seen extensive use internally in &lt;a href="http://www.safewhere.net/"&gt;Safewhere&lt;/a&gt; during
most of this time. It has proven to be very stable, expressive and generally a delight
to use.
&lt;/p&gt;
&lt;p&gt;
If all goes well, the Release Candidate period will be short. Key users have a chance
to veto the this version, but if no-one complains within a week from now, we will
promote RC1 to version 1.0.
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38665"&gt;RC1
release page&lt;/a&gt; has more detail about this particular release.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9d21d46b-ecde-4d81-bfbb-71f1a6e342a7" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,9d21d46b-ecde-4d81-bfbb-71f1a6e342a7.aspx</comments>
      <category>AutoFixture</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=08e6da72-55c6-4059-a7c2-8b4b32779ee3</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,08e6da72-55c6-4059-a7c2-8b4b32779ee3.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,08e6da72-55c6-4059-a7c2-8b4b32779ee3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=08e6da72-55c6-4059-a7c2-8b4b32779ee3</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A reader <a href="http://blog.ploeh.dk/CommentView,guid,292e8e24-b3ae-4805-94c0-9511f23a6ec4.aspx#commentstart">asked
me</a> how <a href="http://autofixture.codeplex.com/">AutoFixture</a> can deal with
arrays as fields on a class. More specifically, given a class like MyClassA, how can
AutoFixture assign a proper array with initialized values to Items?
</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 MyClassA\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 MyClassB\cf0 [] Items;\par ??    \cf1 public\cf0  \cf4 MyClassC\cf0  C;\par ??    \cf1 public\cf0  \cf4 MyClassD\cf0  D;\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">MyClassA</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">MyClassB</span>[]
Items;</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">MyClassC</span> C;</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">MyClassD</span> D;</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Ignoring the bad practice of publicly exposing fields, the main problem is that AutoFixture
has no inherent understanding of arrays, so if we try to create a new instance of
MyClassA by invoking the <a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx">CreateAnonymous</a> method,
we would end up with Items being an array of nulls.
</p>
        <p>
Obviously we want a populated array instead. There are at least a couple of ways to
reach this goal.
</p>
        <p>
The simplest is just to create it and modify it afterwards:
</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  mc = fixture.CreateAnonymous&lt;\cf4 MyClassA\cf0 &gt;();\par ??mc.Items = fixture.CreateMany&lt;\cf4 MyClassB\cf0 &gt;().ToArray();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClassA</span>&gt;();</pre>
          <pre style="margin: 0px">mc.Items = fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray();</pre>
        </div>
        <p>
Although the CreateAnomymous method will assign an unitialized array, we immediately
overwrite the value with an initialized array. The <a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx">CreateMany</a> method
returns an IEnumerable&lt;MyClassB&gt; on which we can use the ToArray extension method
to create an array.
</p>
        <p>
The next option is to do almost the same thing, but as a single operation:
</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  mc = fixture.Build&lt;\cf4 MyClassA\cf0 &gt;()\par ??    .With(x =&gt; x.Items, \par ??        fixture.CreateMany&lt;\cf4 MyClassB\cf0 &gt;().ToArray())\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> mc
= fixture.Build&lt;<span style="color: #2b91af">MyClassA</span>&gt;()</pre>
          <pre style="margin: 0px">    .With(x =&gt; x.Items, </pre>
          <pre style="margin: 0px">        fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray())</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
Besides the different syntax and the lower semicolon count, the biggest difference
is that in this case the Items field is never assigned an unitialized array because <a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx">the
With method ensures that the specified value is assigned immediately</a>.
</p>
        <p>
If you get tired of writing this Builder sequence every time you want to create a
new MyClassA instance, you can <a href="http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx">Customize
the Fixture</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;}??\fs20 fixture.Customize&lt;\cf3 MyClassA\cf0 &gt;(ob =&gt;\par ??    ob.With(x =&gt; x.Items, \par ??        fixture.CreateMany&lt;\cf3 MyClassB\cf0 &gt;().ToArray()));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">fixture.Customize&lt;<span style="color: #2b91af">MyClassA</span>&gt;(ob
=&gt;</pre>
          <pre style="margin: 0px">    ob.With(x =&gt; x.Items, </pre>
          <pre style="margin: 0px">        fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray()));</pre>
        </div>
        <p>
With this customization, every time we invoke
</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  mc = fixture.CreateAnonymous&lt;\cf4 MyClassA\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClassA</span>&gt;();</pre>
        </div>
        <p>
we will get an instance of MyClassA with a properly initialized Items array.
</p>
        <p>
I hope you find one or more of these methods useful.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=08e6da72-55c6-4059-a7c2-8b4b32779ee3" />
      </body>
      <title>Building and assigning arrays with AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,08e6da72-55c6-4059-a7c2-8b4b32779ee3.aspx</guid>
      <link>http://blog.ploeh.dk/2009/12/05/BuildingAndAssigningArraysWithAutoFixture.aspx</link>
      <pubDate>Sat, 05 Dec 2009 00:41:45 GMT</pubDate>
      <description>&lt;p&gt;
A reader &lt;a href="http://blog.ploeh.dk/CommentView,guid,292e8e24-b3ae-4805-94c0-9511f23a6ec4.aspx#commentstart"&gt;asked
me&lt;/a&gt; how &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; can deal with
arrays as fields on a class. More specifically, given a class like MyClassA, how can
AutoFixture assign a proper array with initialized values to Items?
&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 MyClassA\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 MyClassB\cf0 [] Items;\par ??    \cf1 public\cf0  \cf4 MyClassC\cf0  C;\par ??    \cf1 public\cf0  \cf4 MyClassD\cf0  D;\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;MyClassA&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;MyClassB&lt;/span&gt;[]
Items;&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;MyClassC&lt;/span&gt; C;&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;MyClassD&lt;/span&gt; D;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Ignoring the bad practice of publicly exposing fields, the main problem is that AutoFixture
has no inherent understanding of arrays, so if we try to create a new instance of
MyClassA by invoking the &lt;a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx"&gt;CreateAnonymous&lt;/a&gt; method,
we would end up with Items being an array of nulls.
&lt;/p&gt;
&lt;p&gt;
Obviously we want a populated array instead. There are at least a couple of ways to
reach this goal.
&lt;/p&gt;
&lt;p&gt;
The simplest is just to create it and modify it afterwards:
&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  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClassA\cf0 &amp;gt;();\par ??mc.Items = fixture.CreateMany&amp;lt;\cf4 MyClassB\cf0 &amp;gt;().ToArray();}
--&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; mc
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassA&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;mc.Items = fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassB&lt;/span&gt;&amp;gt;().ToArray();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Although the CreateAnomymous method will assign an unitialized array, we immediately
overwrite the value with an initialized array. The &lt;a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx"&gt;CreateMany&lt;/a&gt; method
returns an IEnumerable&amp;lt;MyClassB&amp;gt; on which we can use the ToArray extension method
to create an array.
&lt;/p&gt;
&lt;p&gt;
The next option is to do almost the same thing, but as a single operation:
&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  mc = fixture.Build&amp;lt;\cf4 MyClassA\cf0 &amp;gt;()\par ??    .With(x =&amp;gt; x.Items, \par ??        fixture.CreateMany&amp;lt;\cf4 MyClassB\cf0 &amp;gt;().ToArray())\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; mc
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassA&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(x =&amp;gt; x.Items, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassB&lt;/span&gt;&amp;gt;().ToArray())&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;
Besides the different syntax and the lower semicolon count, the biggest difference
is that in this case the Items field is never assigned an unitialized array because &lt;a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx"&gt;the
With method ensures that the specified value is assigned immediately&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
If you get tired of writing this Builder sequence every time you want to create a
new MyClassA instance, you can &lt;a href="http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx"&gt;Customize
the Fixture&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;}??\fs20 fixture.Customize&amp;lt;\cf3 MyClassA\cf0 &amp;gt;(ob =&amp;gt;\par ??    ob.With(x =&amp;gt; x.Items, \par ??        fixture.CreateMany&amp;lt;\cf3 MyClassB\cf0 &amp;gt;().ToArray()));}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;fixture.Customize&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassA&lt;/span&gt;&amp;gt;(ob
=&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ob.With(x =&amp;gt; x.Items, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassB&lt;/span&gt;&amp;gt;().ToArray()));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
With this customization, every time we invoke
&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  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClassA\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; mc
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClassA&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
we will get an instance of MyClassA with a properly initialized Items array.
&lt;/p&gt;
&lt;p&gt;
I hope you find one or more of these methods useful.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=08e6da72-55c6-4059-a7c2-8b4b32779ee3" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,08e6da72-55c6-4059-a7c2-8b4b32779ee3.aspx</comments>
      <category>AutoFixture</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=73994895-a86a-4ea4-bc41-00ed2e0bc0de</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,73994895-a86a-4ea4-bc41-00ed2e0bc0de.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,73994895-a86a-4ea4-bc41-00ed2e0bc0de.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=73994895-a86a-4ea4-bc41-00ed2e0bc0de</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The <a href="http://autofixture.codeplex.com/WorkItem/View.aspx?WorkItemId=2987">following
feature suggestion</a> was recently posted in the <a href="http://autofixture.codeplex.com/">AutoFixture</a> <a href="http://autofixture.codeplex.com/WorkItem/List.aspx">Issue
Tracker</a> board:
</p>
        <blockquote>
          <p>
"We're using AutoFixture to create random rows of data in our DB. A lot of times though,
it creates strings that are too long for the database columns. It would be nice if
the .With&lt;string&gt; method had an overload that took in a min/max length. We want
the random data, but capped at a limit.
</p>
          <p>
"fixture.Build&lt;MyObject&gt;.With(x = x.MyString, 0, 100);
</p>
          <p>
"As an aside, this is for a project that uses Nhibernate and Fluent Nhibernate, which
has these lengths already defined. I would be nice if AutoFixture could automatically
pick up on that somehow."
</p>
        </blockquote>
        <p>
I think such an feature is an excellent idea, but I don't think I will include in
AutoFixture. Why not?
</p>
        <p>
So far, I have kept the AutoFixture API pretty clean and very generic, and it is my
belief that this is one of the main reasons it is so expressive and flexible. There
are no methods that only work on specific types (such as strings), and I am reluctant
to introduce them now.
</p>
        <p>
In the last six months, I have identified a lot of specialized usage idioms that I
would love to package into a reusable library, but I think that they will pollute
the core AutoFixture API, so I'm going to put those in one or more optional 'add-on'
libraries.
</p>
        <p>
The ability to define strings that are constrained on length could be one such feature,
but rather than wait for a helper library, I will show you have you can implement
such a method yourself.
</p>
        <p>
The first thing we need is a method that can create anonymous strings given length
constraints. One possible implementation is this ConstrainedStringGenerator 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 ConstrainedStringGenerator\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 int\cf0  minimumLength;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 int\cf0  maximumLength;\par ??\par ??    \cf1 public\cf0  ConstrainedStringGenerator(\cf1 int\cf0  minimumLength, \par ??        \cf1 int\cf0  maximumLength)\par ??    \{\par ??        \cf1 if\cf0  (maximumLength &lt; 0)\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "..."\cf0 );\par ??        \}\par ??        \cf1 if\cf0  (minimumLength &gt; maximumLength)\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "..."\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .minimumLength = minimumLength;\par ??        \cf1 this\cf0 .maximumLength = maximumLength;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  CreateaAnonymous(\cf1 string\cf0  seed)\par ??    \{\par ??        \cf1 var\cf0  s = \cf1 string\cf0 .Empty;\par ??        \cf1 while\cf0  (s.Length &lt; \cf1 this\cf0 .minimumLength)\par ??        \{\par ??            s += \cf4 GuidStringGenerator\cf0 .CreateAnonymous(seed);\par ??        \}\par ??        \cf1 if\cf0  (s.Length &gt; \cf1 this\cf0 .maximumLength)\par ??        \{\par ??            s = s.Substring(0, \cf1 this\cf0 .maximumLength);\par ??        \}\par ??        \cf1 return\cf0  s;\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">ConstrainedStringGenerator</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">int</span> minimumLength;</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: blue">int</span> maximumLength;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> ConstrainedStringGenerator(<span style="color: blue">int</span> minimumLength, </pre>
          <pre style="margin: 0px">        <span style="color: blue">int</span> maximumLength)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (maximumLength
&lt; 0)</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">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"..."</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (minimumLength
&gt; maximumLength)</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">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"..."</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.minimumLength
= minimumLength;</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.maximumLength
= maximumLength;</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> CreateaAnonymous(<span style="color: blue">string</span> seed)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> s
= <span style="color: blue">string</span>.Empty;</pre>
          <pre style="margin: 0px">        <span style="color: blue">while</span> (s.Length
&lt; <span style="color: blue">this</span>.minimumLength)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            s += <span style="color: #2b91af">GuidStringGenerator</span>.CreateAnonymous(seed);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (s.Length
&gt; <span style="color: blue">this</span>.maximumLength)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            s = s.Substring(0, <span style="color: blue">this</span>.maximumLength);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> s;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The CreateAnonymous method uses AutoFixture's GuidStringGenerator class to create
anonymous strings of the required length. For this implementation I chose a basic
algorithm, but I'm sure you can create one that is more sophisticated if you need
it.
</p>
        <p>
Th next thing we need to do is to implement the desired With method. That can be done
with an extension method works on ObjectBuilder&lt;T&gt;:
</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 ObjectBuilderExtension\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf4 ObjectBuilder\cf0 &lt;T&gt; With&lt;T&gt;(\par ??        \cf1 this\cf0  \cf4 ObjectBuilder\cf0 &lt;T&gt; ob,\par ??        \cf4 Expression\cf0 &lt;\cf4 Func\cf0 &lt;T, \cf1 string\cf0 &gt;&gt; propertyPicker,\par ??        \cf1 int\cf0  minimumLength,\par ??        \cf1 int\cf0  maximumLength)\par ??    \{\par ??        \cf1 var\cf0  me = (\cf4 MemberExpression\cf0 )propertyPicker.Body;\par ??        \cf1 var\cf0  name = me.Member.Name;\par ??        \cf1 var\cf0  generator =\par ??            \cf1 new\cf0  \cf4 ConstrainedStringGenerator\cf0 (\par ??                minimumLength, maximumLength);\par ??        \cf1 var\cf0  value = generator.CreateaAnonymous(name);\par ??        \cf1 return\cf0  ob.With(propertyPicker, value);\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">ObjectBuilderExtension</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">ObjectBuilder</span>&lt;T&gt;
With&lt;T&gt;(</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span><span style="color: #2b91af">ObjectBuilder</span>&lt;T&gt;
ob,</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T, <span style="color: blue">string</span>&gt;&gt;
propertyPicker,</pre>
          <pre style="margin: 0px">        <span style="color: blue">int</span> minimumLength,</pre>
          <pre style="margin: 0px">        <span style="color: blue">int</span> maximumLength)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> me
= (<span style="color: #2b91af">MemberExpression</span>)propertyPicker.Body;</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> name
= me.Member.Name;</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> generator
=</pre>
          <pre style="margin: 0px">            <span style="color: blue">new</span><span style="color: #2b91af">ConstrainedStringGenerator</span>(</pre>
          <pre style="margin: 0px">                minimumLength, maximumLength);</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> value
= generator.CreateaAnonymous(name);</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> ob.With(propertyPicker,
value);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The method takes the same input as ObjectBuilder&lt;T&gt;'s With method, plus the
two integers that constrain the length. Note that the propertyPicker expression has
been constrained to deal only with strings.
</p>
        <p>
In this implementation, we use the ConstrainedStringGenerator class to generate the
desired value for the property, where after we can use the existing With method to
assign the value to the property in question.
</p>
        <p>
This now allows us to write Build statements like the one originally requested:
</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  mc = fixture.Build&lt;\cf4 MyClass\cf0 &gt;()\par ??    .With(x =&gt; x.SomeText, 0, 100)\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> mc
= fixture.Build&lt;<span style="color: #2b91af">MyClass</span>&gt;()</pre>
          <pre style="margin: 0px">    .With(x =&gt; x.SomeText, 0, 100)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
The other part of the request, regarding NHibernate integration, I will leave to the
interested reader – mostly because I have never used NHibernate, so I have no clue
how to do it. I would, however, love to see a blog post with that addition.
</p>
        <p>
This entire example is now part of the AutoFixture test suite, so if you are interested
in looking at it in more detail, you can get it from the AutoFixture source code (available
at CodePlex).
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=73994895-a86a-4ea4-bc41-00ed2e0bc0de" />
      </body>
      <title>Creating length-constrained strings with AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,73994895-a86a-4ea4-bc41-00ed2e0bc0de.aspx</guid>
      <link>http://blog.ploeh.dk/2009/11/07/CreatingLengthconstrainedStringsWithAutoFixture.aspx</link>
      <pubDate>Sat, 07 Nov 2009 19:56:05 GMT</pubDate>
      <description>&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/WorkItem/View.aspx?WorkItemId=2987"&gt;following
feature suggestion&lt;/a&gt; was recently posted in the &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;&amp;nbsp;&lt;a href="http://autofixture.codeplex.com/WorkItem/List.aspx"&gt;Issue
Tracker&lt;/a&gt; board:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
"We're using AutoFixture to create random rows of data in our DB. A lot of times though,
it creates strings that are too long for the database columns. It would be nice if
the .With&amp;lt;string&amp;gt; method had an overload that took in a min/max length. We want
the random data, but capped at a limit.
&lt;/p&gt;
&lt;p&gt;
"fixture.Build&amp;lt;MyObject&amp;gt;.With(x = x.MyString, 0, 100);
&lt;/p&gt;
&lt;p&gt;
"As an aside, this is for a project that uses Nhibernate and Fluent Nhibernate, which
has these lengths already defined. I would be nice if AutoFixture could automatically
pick up on that somehow."
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I think such an feature is an excellent idea, but I don't think I will include in
AutoFixture. Why not?
&lt;/p&gt;
&lt;p&gt;
So far, I have kept the AutoFixture API pretty clean and very generic, and it is my
belief that this is one of the main reasons it is so expressive and flexible. There
are no methods that only work on specific types (such as strings), and I am reluctant
to introduce them now.
&lt;/p&gt;
&lt;p&gt;
In the last six months, I have identified a lot of specialized usage idioms that I
would love to package into a reusable library, but I think that they will pollute
the core AutoFixture API, so I'm going to put those in one or more optional 'add-on'
libraries.
&lt;/p&gt;
&lt;p&gt;
The ability to define strings that are constrained on length could be one such feature,
but rather than wait for a helper library, I will show you have you can implement
such a method yourself.
&lt;/p&gt;
&lt;p&gt;
The first thing we need is a method that can create anonymous strings given length
constraints. One possible implementation is this ConstrainedStringGenerator 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 ConstrainedStringGenerator\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 int\cf0  minimumLength;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf1 int\cf0  maximumLength;\par ??\par ??    \cf1 public\cf0  ConstrainedStringGenerator(\cf1 int\cf0  minimumLength, \par ??        \cf1 int\cf0  maximumLength)\par ??    \{\par ??        \cf1 if\cf0  (maximumLength &amp;lt; 0)\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "..."\cf0 );\par ??        \}\par ??        \cf1 if\cf0  (minimumLength &amp;gt; maximumLength)\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "..."\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .minimumLength = minimumLength;\par ??        \cf1 this\cf0 .maximumLength = maximumLength;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  CreateaAnonymous(\cf1 string\cf0  seed)\par ??    \{\par ??        \cf1 var\cf0  s = \cf1 string\cf0 .Empty;\par ??        \cf1 while\cf0  (s.Length &amp;lt; \cf1 this\cf0 .minimumLength)\par ??        \{\par ??            s += \cf4 GuidStringGenerator\cf0 .CreateAnonymous(seed);\par ??        \}\par ??        \cf1 if\cf0  (s.Length &amp;gt; \cf1 this\cf0 .maximumLength)\par ??        \{\par ??            s = s.Substring(0, \cf1 this\cf0 .maximumLength);\par ??        \}\par ??        \cf1 return\cf0  s;\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;ConstrainedStringGenerator&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;int&lt;/span&gt; minimumLength;&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;int&lt;/span&gt; maximumLength;&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; ConstrainedStringGenerator(&lt;span style="color: blue"&gt;int&lt;/span&gt; minimumLength, &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; maximumLength)&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; (maximumLength
&amp;lt; 0)&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;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"..."&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; (minimumLength
&amp;gt; maximumLength)&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;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"..."&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;.minimumLength
= minimumLength;&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;.maximumLength
= maximumLength;&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; CreateaAnonymous(&lt;span style="color: blue"&gt;string&lt;/span&gt; seed)&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;var&lt;/span&gt; s
= &lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty;&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;while&lt;/span&gt; (s.Length
&amp;lt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.minimumLength)&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; s += &lt;span style="color: #2b91af"&gt;GuidStringGenerator&lt;/span&gt;.CreateAnonymous(seed);&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; (s.Length
&amp;gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.maximumLength)&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; s = s.Substring(0, &lt;span style="color: blue"&gt;this&lt;/span&gt;.maximumLength);&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;return&lt;/span&gt; s;&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 CreateAnonymous method uses AutoFixture's GuidStringGenerator class to create
anonymous strings of the required length. For this implementation I chose a basic
algorithm, but I'm sure you can create one that is more sophisticated if you need
it.
&lt;/p&gt;
&lt;p&gt;
Th next thing we need to do is to implement the desired With method. That can be done
with an extension method works on ObjectBuilder&amp;lt;T&amp;gt;:
&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 ObjectBuilderExtension\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf4 ObjectBuilder\cf0 &amp;lt;T&amp;gt; With&amp;lt;T&amp;gt;(\par ??        \cf1 this\cf0  \cf4 ObjectBuilder\cf0 &amp;lt;T&amp;gt; ob,\par ??        \cf4 Expression\cf0 &amp;lt;\cf4 Func\cf0 &amp;lt;T, \cf1 string\cf0 &amp;gt;&amp;gt; propertyPicker,\par ??        \cf1 int\cf0  minimumLength,\par ??        \cf1 int\cf0  maximumLength)\par ??    \{\par ??        \cf1 var\cf0  me = (\cf4 MemberExpression\cf0 )propertyPicker.Body;\par ??        \cf1 var\cf0  name = me.Member.Name;\par ??        \cf1 var\cf0  generator =\par ??            \cf1 new\cf0  \cf4 ConstrainedStringGenerator\cf0 (\par ??                minimumLength, maximumLength);\par ??        \cf1 var\cf0  value = generator.CreateaAnonymous(name);\par ??        \cf1 return\cf0  ob.With(propertyPicker, value);\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;ObjectBuilderExtension&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;ObjectBuilder&lt;/span&gt;&amp;lt;T&amp;gt;
With&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; &lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ObjectBuilder&lt;/span&gt;&amp;lt;T&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; &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, &lt;span style="color: blue"&gt;string&lt;/span&gt;&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;int&lt;/span&gt; minimumLength,&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; maximumLength)&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;var&lt;/span&gt; me
= (&lt;span style="color: #2b91af"&gt;MemberExpression&lt;/span&gt;)propertyPicker.Body;&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;var&lt;/span&gt; name
= me.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;var&lt;/span&gt; generator
=&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;ConstrainedStringGenerator&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; minimumLength, maximumLength);&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;var&lt;/span&gt; value
= generator.CreateaAnonymous(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; ob.With(propertyPicker,
value);&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 method takes the same input as ObjectBuilder&amp;lt;T&amp;gt;'s With method, plus the
two integers that constrain the length. Note that the propertyPicker expression has
been constrained to deal only with strings.
&lt;/p&gt;
&lt;p&gt;
In this implementation, we use the ConstrainedStringGenerator class to generate the
desired value for the property, where after we can use the existing With method to
assign the value to the property in question.
&lt;/p&gt;
&lt;p&gt;
This now allows us to write Build statements like the one originally requested:
&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  mc = fixture.Build&amp;lt;\cf4 MyClass\cf0 &amp;gt;()\par ??    .With(x =&amp;gt; x.SomeText, 0, 100)\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; mc
= fixture.Build&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; .With(x =&amp;gt; x.SomeText, 0, 100)&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;
The other part of the request, regarding NHibernate integration, I will leave to the
interested reader – mostly because I have never used NHibernate, so I have no clue
how to do it. I would, however, love to see a blog post with that addition.
&lt;/p&gt;
&lt;p&gt;
This entire example is now part of the AutoFixture test suite, so if you are interested
in looking at it in more detail, you can get it from the AutoFixture source code (available
at CodePlex).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=73994895-a86a-4ea4-bc41-00ed2e0bc0de" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,73994895-a86a-4ea4-bc41-00ed2e0bc0de.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2b0fb814-4d75-422b-a104-896bf3de1ed4</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2b0fb814-4d75-422b-a104-896bf3de1ed4.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2b0fb814-4d75-422b-a104-896bf3de1ed4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2b0fb814-4d75-422b-a104-896bf3de1ed4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> beta 1 is now available
on the CodePlex site! We have been using AutoFixture quite intensively in <a href="http://www.safewhere.net">Safewhere</a> for
almost half a year now, and the core of it has turned out to be stable and much more
powerful than I originally imagined.
</p>
        <p>
During that period, I have discovered and fixed a few bugs, but the most positive
experience has been how extended usage has inspired us to come up with numerous ideas
to new cool features. Some of these features are already implemented in the current
release, and the rest are <a href="http://autofixture.codeplex.com/WorkItem/List.aspx">listed
on the AutoFixture site</a>.
</p>
        <p>
The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29097">beta
1 release page</a> has more details about this particular release.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2b0fb814-4d75-422b-a104-896bf3de1ed4" />
      </body>
      <title>AutoFixture beta 1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2b0fb814-4d75-422b-a104-896bf3de1ed4.aspx</guid>
      <link>http://blog.ploeh.dk/2009/10/31/AutoFixtureBeta1.aspx</link>
      <pubDate>Sat, 31 Oct 2009 08:59:43 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; beta 1 is now available
on the CodePlex site! We have been using AutoFixture quite intensively in &lt;a href="http://www.safewhere.net"&gt;Safewhere&lt;/a&gt; for
almost half a year now, and the core of it has turned out to be stable and much more
powerful than I originally imagined.
&lt;/p&gt;
&lt;p&gt;
During that period, I have discovered and fixed a few bugs, but the most positive
experience has been how extended usage has inspired us to come up with numerous ideas
to new cool features. Some of these features are already implemented in the current
release, and the rest are &lt;a href="http://autofixture.codeplex.com/WorkItem/List.aspx"&gt;listed
on the AutoFixture site&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29097"&gt;beta
1 release page&lt;/a&gt; has more details about this particular release.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2b0fb814-4d75-422b-a104-896bf3de1ed4" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2b0fb814-4d75-422b-a104-896bf3de1ed4.aspx</comments>
      <category>AutoFixture</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=0b1d8171-002d-49f9-be17-ee0007333451</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,0b1d8171-002d-49f9-be17-ee0007333451.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,0b1d8171-002d-49f9-be17-ee0007333451.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=0b1d8171-002d-49f9-be17-ee0007333451</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the <a href="http://blog.ploeh.dk/2009/08/25/DoRedux.aspx">previous post</a> on <a href="http://autofixture.codeplex.com/">AutoFixture</a>,
I demonstrated how it's possible to use a customized Builder to perform complex initialization
when requesting an instance of a particular type. To recap, this was the solution
I described:
</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  mc = fixture.CreateAnonymous&lt;\cf4 MyClass\cf0 &gt;();\par ??\cf1 var\cf0  mvm = fixture.Build&lt;\cf4 MyViewModel\cf0 &gt;()\par ??    .Do(x =&gt; x.AvailableItems.Add(mc))\par ??    .With(x =&gt; x.SelectedItem, mc)\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> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mvm
= fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;()</pre>
          <pre style="margin: 0px">    .Do(x =&gt; x.AvailableItems.Add(mc))</pre>
          <pre style="margin: 0px">    .With(x =&gt; x.SelectedItem, mc)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
This code first creates an anonymous instance of MyClass that can be added to MyViewModel.
It then initializes a Builder for a specific instance of MyViewModel, instructing
it to
</p>
        <ol>
          <li>
add the anonymous MyClass instance to the list of AvailableItems 
</li>
          <li>
assign the same instance to the SelectedItem property</li>
        </ol>
        <p>
While this works splendidly, it can get tiresome to write the same customization over
and over again if you need to create multiple instances of the same type. It also
violate the DRY principle.
</p>
        <p>
When this is the case, you can alternatively register a customized Builder pipeline
for the type in question (in this case MyViewModel). This is done with the Customize
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 var\cf0  mc = fixture.CreateAnonymous&lt;\cf4 MyClass\cf0 &gt;();\par ??fixture.Customize&lt;\cf4 MyViewModel\cf0 &gt;(ob =&gt; ob\par ??    .Do(x =&gt; x.AvailableItems.Add(mc))\par ??    .With(x =&gt; x.SelectedItem, mc));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">fixture.Customize&lt;<span style="color: #2b91af">MyViewModel</span>&gt;(ob
=&gt; ob</pre>
          <pre style="margin: 0px">    .Do(x =&gt; x.AvailableItems.Add(mc))</pre>
          <pre style="margin: 0px">    .With(x =&gt; x.SelectedItem, mc));</pre>
        </div>
        <p>
The Customize method takes as input a function that provides an initial ObjectBuilder
as input, and returns a new, customized ObjectBuilder as output. This function is
registered with the type, so that each time an anonymous instance of the type is requested,
the customized ObjectBuilder will be used to create the instance.
</p>
        <p>
In the example, I customize the supplied ObjectBuilder (<em>ob</em>) in exactly the
same way as before, but instead of invoking CreateAnonymous, I simply return the customized
ObjectBuilder to the Fixture instance. It then saves this customized ObjectBuilder
for later use.
</p>
        <p>
With this customization, what before failed now succeeds:
</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  mvm = fixture.CreateAnonymous&lt;\cf4 MyViewModel\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mvm
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;();</pre>
        </div>
        <p>
The Customize method is the core method for customizing AutoFixture. Most other customization
methods (like Register) are simply convenience methods that wraps Customize. It is
a very powerful method that can be used to define some very specific Builder algorithms
for particular types.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0b1d8171-002d-49f9-be17-ee0007333451" />
      </body>
      <title>Customizing A Type's Builder With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,0b1d8171-002d-49f9-be17-ee0007333451.aspx</guid>
      <link>http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx</link>
      <pubDate>Tue, 22 Sep 2009 14:53:48 GMT</pubDate>
      <description>&lt;p&gt;
In the &lt;a href="http://blog.ploeh.dk/2009/08/25/DoRedux.aspx"&gt;previous post&lt;/a&gt; on &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;,
I demonstrated how it's possible to use a customized Builder to perform complex initialization
when requesting an instance of a particular type. To recap, this was the solution
I described:
&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  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??\cf1 var\cf0  mvm = fixture.Build&amp;lt;\cf4 MyViewModel\cf0 &amp;gt;()\par ??    .Do(x =&amp;gt; x.AvailableItems.Add(mc))\par ??    .With(x =&amp;gt; x.SelectedItem, mc)\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; mc
= 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;&lt;span style="color: blue"&gt;var&lt;/span&gt; mvm
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Do(x =&amp;gt; x.AvailableItems.Add(mc))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(x =&amp;gt; x.SelectedItem, mc)&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;
This code first creates an anonymous instance of MyClass that can be added to MyViewModel.
It then initializes a Builder for a specific instance of MyViewModel, instructing
it to
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
add the anonymous MyClass instance to the list of AvailableItems 
&lt;li&gt;
assign the same instance to the SelectedItem property&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
While this works splendidly, it can get tiresome to write the same customization over
and over again if you need to create multiple instances of the same type. It also
violate the DRY principle.
&lt;/p&gt;
&lt;p&gt;
When this is the case, you can alternatively register a customized Builder pipeline
for the type in question (in this case MyViewModel). This is done with the Customize
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 var\cf0  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??fixture.Customize&amp;lt;\cf4 MyViewModel\cf0 &amp;gt;(ob =&amp;gt; ob\par ??    .Do(x =&amp;gt; x.AvailableItems.Add(mc))\par ??    .With(x =&amp;gt; x.SelectedItem, mc));}
--&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; mc
= 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;fixture.Customize&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;(ob
=&amp;gt; ob&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Do(x =&amp;gt; x.AvailableItems.Add(mc))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(x =&amp;gt; x.SelectedItem, mc));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The Customize method takes as input a function that provides an initial ObjectBuilder
as input, and returns a new, customized ObjectBuilder as output. This function is
registered with the type, so that each time an anonymous instance of the type is requested,
the customized ObjectBuilder will be used to create the instance.
&lt;/p&gt;
&lt;p&gt;
In the example, I customize the supplied ObjectBuilder (&lt;em&gt;ob&lt;/em&gt;) in exactly the
same way as before, but instead of invoking CreateAnonymous, I simply return the customized
ObjectBuilder to the Fixture instance. It then saves this customized ObjectBuilder
for later use.
&lt;/p&gt;
&lt;p&gt;
With this customization, what before failed now succeeds:
&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  mvm = fixture.CreateAnonymous&amp;lt;\cf4 MyViewModel\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; mvm
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The Customize method is the core method for customizing AutoFixture. Most other customization
methods (like Register) are simply convenience methods that wraps Customize. It is
a very powerful method that can be used to define some very specific Builder algorithms
for particular types.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0b1d8171-002d-49f9-be17-ee0007333451" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,0b1d8171-002d-49f9-be17-ee0007333451.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=027e9d81-081f-4c81-b9d6-f922550cd2fa</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,027e9d81-081f-4c81-b9d6-f922550cd2fa.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,027e9d81-081f-4c81-b9d6-f922550cd2fa.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=027e9d81-081f-4c81-b9d6-f922550cd2fa</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Yesterday I released version .8.6 of <a href="http://autofixture.codeplex.com/">AutoFixture</a>.
It is a minor release that simply adds some new features.
</p>
        <p>
There are some minor breaking changes (documented on the <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33256">release
page</a>), but they only affect supporting classes and don't touch on any of the code
examples I have so far published. In other words, if you are using AutoFixture's fluent
interface, your code should still compile.
</p>
        <p>
Please go ahead and download it and use it. As always, comments and questions are
welcome, either here or in the <a href="http://autofixture.codeplex.com/Thread/List.aspx">forum</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=027e9d81-081f-4c81-b9d6-f922550cd2fa" />
      </body>
      <title>AutoFixture .8.6 Released</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,027e9d81-081f-4c81-b9d6-f922550cd2fa.aspx</guid>
      <link>http://blog.ploeh.dk/2009/09/21/AutoFixture86Released.aspx</link>
      <pubDate>Mon, 21 Sep 2009 17:37:36 GMT</pubDate>
      <description>&lt;p&gt;
Yesterday I released version .8.6 of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;.
It is a minor release that simply adds some new features.
&lt;/p&gt;
&lt;p&gt;
There are some minor breaking changes (documented on the &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33256"&gt;release
page&lt;/a&gt;), but they only affect supporting classes and don't touch on any of the code
examples I have so far published. In other words, if you are using AutoFixture's fluent
interface, your code should still compile.
&lt;/p&gt;
&lt;p&gt;
Please go ahead and download it and use it. As always, comments and questions are
welcome, either here or in the &lt;a href="http://autofixture.codeplex.com/Thread/List.aspx"&gt;forum&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=027e9d81-081f-4c81-b9d6-f922550cd2fa" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,027e9d81-081f-4c81-b9d6-f922550cd2fa.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=146f7340-2e70-44ad-9b74-35e86432d2c7</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,146f7340-2e70-44ad-9b74-35e86432d2c7.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,146f7340-2e70-44ad-9b74-35e86432d2c7.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=146f7340-2e70-44ad-9b74-35e86432d2c7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It gives me great pleasure to announce that I have just release version .8.5 of <a href="http://autofixture.codeplex.com/">AutoFixture</a>.
It is a minor release (hence the numbering) that mainly contains a lot of convenience
overloads to already existing methods. There is also a single bug fix.
</p>
        <p>
There are two breaking changes (documented on the <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=31587">release
page</a>), but they are minor and I do not expect them to cause problems. Only one
of these even remotely affects any part of the API I have already discussed here,
and that relates to what kind of exception is being thrown <a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx">when
AutoFixture is unable to create an instance of the requested type</a>.
</p>
        <p>
Please go ahead and download it and use it heavily :) As always, comments and questions
are welcome.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=146f7340-2e70-44ad-9b74-35e86432d2c7" />
      </body>
      <title>AutoFixture .8.5 Released</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,146f7340-2e70-44ad-9b74-35e86432d2c7.aspx</guid>
      <link>http://blog.ploeh.dk/2009/09/02/AutoFixture85Released.aspx</link>
      <pubDate>Wed, 02 Sep 2009 20:21:13 GMT</pubDate>
      <description>&lt;p&gt;
It gives me great pleasure to announce that I have just release version .8.5 of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;.
It is a minor release (hence the numbering) that mainly contains a lot of convenience
overloads to already existing methods. There is also a single bug fix.
&lt;/p&gt;
&lt;p&gt;
There are two breaking changes (documented on the &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=31587"&gt;release
page&lt;/a&gt;), but they are minor and I do not expect them to cause problems. Only one
of these even remotely affects any part of the API I have already discussed here,
and that relates to what kind of exception is being thrown &lt;a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx"&gt;when
AutoFixture is unable to create an instance of the requested type&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Please go ahead and download it and use it heavily :) As always, comments and questions
are welcome.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=146f7340-2e70-44ad-9b74-35e86432d2c7" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,146f7340-2e70-44ad-9b74-35e86432d2c7.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=292e8e24-b3ae-4805-94c0-9511f23a6ec4</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,292e8e24-b3ae-4805-94c0-9511f23a6ec4.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,292e8e24-b3ae-4805-94c0-9511f23a6ec4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=292e8e24-b3ae-4805-94c0-9511f23a6ec4</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Soon after I posted <a href="http://blog.ploeh.dk/2009/06/09/CallingMethodsWhileBuildingAnonymousVariablesWithAutoFixture.aspx">my
post on the AutoFixture Custom Builder's Do method</a>, a much better example occurred
to me, so let's revisit this feature in light of a more reasonable context.
</p>
        <p>
When I write WPF code, I always use the <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx">MVVM
pattern</a>. When I need to create a Master/Detail View, I usually model it so that
my View Model has a list of available items, and a property that returns the currently
selected item. In this way, I can bind the current Detail View to the currently selected
item purely through the View Model.
</p>
        <p>
Such a View Model might look 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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 MyViewModel\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 List\cf0 &lt;\cf4 MyClass\cf0 &gt; availableItems;\par ??    \cf1 private\cf0  \cf4 MyClass\cf0  selectedItem;\par ??\par ??    \cf1 public\cf0  MyViewModel()\par ??    \{\par ??        \cf1 this\cf0 .availableItems = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 MyClass\cf0 &gt;();\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 ICollection\cf0 &lt;\cf4 MyClass\cf0 &gt; AvailableItems\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .availableItems; \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 MyClass\cf0  SelectedItem\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .selectedItem; \}\par ??        \cf1 set\cf0  \par ??        \{\par ??            \cf1 if\cf0  (!\cf1 this\cf0 .availableItems.Contains(\cf1 value\cf0 ))\par ??            \{\par ??                \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentException\cf0 (\cf5 "..."\cf0 );\par ??            \}\par ??            \cf1 this\cf0 .selectedItem = \cf1 value\cf0 ;\par ??        \}\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">MyViewModel</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">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;
availableItems;</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: #2b91af">MyClass</span> selectedItem;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> MyViewModel()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.availableItems
= <span style="color: blue">new</span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">ICollection</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;
AvailableItems</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>.availableItems;
}</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">MyClass</span> SelectedItem</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>.selectedItem;
}</pre>
          <pre style="margin: 0px">        <span style="color: blue">set</span></pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">if</span> (!<span style="color: blue">this</span>.availableItems.Contains(<span style="color: blue">value</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">ArgumentException</span>(<span style="color: #a31515">"..."</span>);</pre>
          <pre style="margin: 0px">            }</pre>
          <pre style="margin: 0px">            <span style="color: blue">this</span>.selectedItem
= <span style="color: blue">value</span>;</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The main point of interest is that if you attempt to set SelectedItem to an instance
that's not contained in the list of available items, an exception will be thrown.
That's reasonable behavior, since we want the user to select only from the available
items.
</p>
        <p>
By default, <a href="http://autofixture.codeplex.com/">AutoFixture</a> works by assigning
an Anonymous Value to all writable properties. Since these values are auto-generated,
the value AutoFixture is going to assign to SelectedItem will be a new instance of
MyClass, and thus not one of the available items. In other words, this will throw
an exception:
</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  mvm = fixture.CreateAnonymous&lt;\cf4 MyViewModel\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mvm
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;();</pre>
        </div>
        <p>
There are several solutions to this situation, depending on the scenario. If you need
an instance with SelectedItem correctly set to a non-null value, you can use the Do
method 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  mc = fixture.CreateAnonymous&lt;\cf4 MyClass\cf0 &gt;();\par ??\cf1 var\cf0  mvm = fixture.Build&lt;\cf4 MyViewModel\cf0 &gt;()\par ??    .Do(x =&gt; x.AvailableItems.Add(mc))\par ??    .With(x =&gt; x.SelectedItem, mc)\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> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mvm
= fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;()</pre>
          <pre style="margin: 0px">    .Do(x =&gt; x.AvailableItems.Add(mc))</pre>
          <pre style="margin: 0px">    .With(x =&gt; x.SelectedItem, mc)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
This first creates an anonymous instance of MyClass, adds it to AvailableItems as
part of a customized Builder pipeline and subsequently assigns it to SelectedItem.
</p>
        <p>
Another option is to skip assigning only the SelectedItem property. This is a good
option if you don't need that value in a particular test. You can use the <a href="http://blog.ploeh.dk/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture.aspx">Without</a> method
to do that:
</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  mvm = fixture.Build&lt;\cf4 MyViewModel\cf0 &gt;()\par ??    .Without(s =&gt; s.SelectedItem)\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> mvm
= fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;()</pre>
          <pre style="margin: 0px">    .Without(s =&gt; s.SelectedItem)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
This will assign a value to all other writable properties of MyViewModel (if it had
had any), except the SelectedItem property. In this case, the value of SelectedItem
will be null, since it is being ignored.
</p>
        <p>
Finally you can simply choose to omit all AutoProperties using the <a href="http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx">OmitAutoProperties</a> 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 var\cf0  mvm = fixture.Build&lt;\cf4 MyViewModel\cf0 &gt;()\par ??    .OmitAutoProperties()\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> mvm
= fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;()</pre>
          <pre style="margin: 0px">    .OmitAutoProperties()</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
In this scenario, only MyViewModel's constructor is being executed, while all writable
properties are being ignored.
</p>
        <p>
As you can see, AutoFixture offers great flexibility in providing specialized custom
Builders that fit almost any situation.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=292e8e24-b3ae-4805-94c0-9511f23a6ec4" />
      </body>
      <title>Do Redux</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,292e8e24-b3ae-4805-94c0-9511f23a6ec4.aspx</guid>
      <link>http://blog.ploeh.dk/2009/08/25/DoRedux.aspx</link>
      <pubDate>Tue, 25 Aug 2009 18:27:39 GMT</pubDate>
      <description>&lt;p&gt;
Soon after I posted &lt;a href="http://blog.ploeh.dk/2009/06/09/CallingMethodsWhileBuildingAnonymousVariablesWithAutoFixture.aspx"&gt;my
post on the AutoFixture Custom Builder's Do method&lt;/a&gt;, a much better example occurred
to me, so let's revisit this feature in light of a more reasonable context.
&lt;/p&gt;
&lt;p&gt;
When I write WPF code, I always use the &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx"&gt;MVVM
pattern&lt;/a&gt;. When I need to create a Master/Detail View, I usually model it so that
my View Model has a list of available items, and a property that returns the currently
selected item. In this way, I can bind the current Detail View to the currently selected
item purely through the View Model.
&lt;/p&gt;
&lt;p&gt;
Such a View Model might look 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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 MyViewModel\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 List\cf0 &amp;lt;\cf4 MyClass\cf0 &amp;gt; availableItems;\par ??    \cf1 private\cf0  \cf4 MyClass\cf0  selectedItem;\par ??\par ??    \cf1 public\cf0  MyViewModel()\par ??    \{\par ??        \cf1 this\cf0 .availableItems = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 ICollection\cf0 &amp;lt;\cf4 MyClass\cf0 &amp;gt; AvailableItems\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .availableItems; \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 MyClass\cf0  SelectedItem\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .selectedItem; \}\par ??        \cf1 set\cf0  \par ??        \{\par ??            \cf1 if\cf0  (!\cf1 this\cf0 .availableItems.Contains(\cf1 value\cf0 ))\par ??            \{\par ??                \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentException\cf0 (\cf5 "..."\cf0 );\par ??            \}\par ??            \cf1 this\cf0 .selectedItem = \cf1 value\cf0 ;\par ??        \}\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;MyViewModel&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;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;
availableItems;&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;MyClass&lt;/span&gt; selectedItem;&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; MyViewModel()&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;.availableItems
= &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;MyClass&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;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;
AvailableItems&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;.availableItems;
}&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: #2b91af"&gt;MyClass&lt;/span&gt; SelectedItem&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;.selectedItem;
}&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;set&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;if&lt;/span&gt; (!&lt;span style="color: blue"&gt;this&lt;/span&gt;.availableItems.Contains(&lt;span style="color: blue"&gt;value&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; &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;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"..."&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; &lt;span style="color: blue"&gt;this&lt;/span&gt;.selectedItem
= &lt;span style="color: blue"&gt;value&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; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The main point of interest is that if you attempt to set SelectedItem to an instance
that's not contained in the list of available items, an exception will be thrown.
That's reasonable behavior, since we want the user to select only from the available
items.
&lt;/p&gt;
&lt;p&gt;
By default, &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; works by assigning
an Anonymous Value to all writable properties. Since these values are auto-generated,
the value AutoFixture is going to assign to SelectedItem will be a new instance of
MyClass, and thus not one of the available items. In other words, this will throw
an exception:
&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  mvm = fixture.CreateAnonymous&amp;lt;\cf4 MyViewModel\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; mvm
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
There are several solutions to this situation, depending on the scenario. If you need
an instance with SelectedItem correctly set to a non-null value, you can use the Do
method 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  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??\cf1 var\cf0  mvm = fixture.Build&amp;lt;\cf4 MyViewModel\cf0 &amp;gt;()\par ??    .Do(x =&amp;gt; x.AvailableItems.Add(mc))\par ??    .With(x =&amp;gt; x.SelectedItem, mc)\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; mc
= 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;&lt;span style="color: blue"&gt;var&lt;/span&gt; mvm
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Do(x =&amp;gt; x.AvailableItems.Add(mc))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(x =&amp;gt; x.SelectedItem, mc)&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;
This first creates an anonymous instance of MyClass, adds it to AvailableItems as
part of a customized Builder pipeline and subsequently assigns it to SelectedItem.
&lt;/p&gt;
&lt;p&gt;
Another option is to skip assigning only the SelectedItem property. This is a good
option if you don't need that value in a particular test. You can use the &lt;a href="http://blog.ploeh.dk/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture.aspx"&gt;Without&lt;/a&gt; method
to do that:
&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  mvm = fixture.Build&amp;lt;\cf4 MyViewModel\cf0 &amp;gt;()\par ??    .Without(s =&amp;gt; s.SelectedItem)\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; mvm
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Without(s =&amp;gt; s.SelectedItem)&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;
This will assign a value to all other writable properties of MyViewModel (if it had
had any), except the SelectedItem property. In this case, the value of SelectedItem
will be null, since it is being ignored.
&lt;/p&gt;
&lt;p&gt;
Finally you can simply choose to omit all AutoProperties using the &lt;a href="http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx"&gt;OmitAutoProperties&lt;/a&gt; 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 var\cf0  mvm = fixture.Build&amp;lt;\cf4 MyViewModel\cf0 &amp;gt;()\par ??    .OmitAutoProperties()\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; mvm
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&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; .CreateAnonymous();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this scenario, only MyViewModel's constructor is being executed, while all writable
properties are being ignored.
&lt;/p&gt;
&lt;p&gt;
As you can see, AutoFixture offers great flexibility in providing specialized custom
Builders that fit almost any situation.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=292e8e24-b3ae-4805-94c0-9511f23a6ec4" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,292e8e24-b3ae-4805-94c0-9511f23a6ec4.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=0fd21249-f8d1-4095-b428-42db301f63cc</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,0fd21249-f8d1-4095-b428-42db301f63cc.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,0fd21249-f8d1-4095-b428-42db301f63cc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=0fd21249-f8d1-4095-b428-42db301f63cc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The default behavior of <a href="http://autofixture.codeplex.com/">AutoFixture</a> is
to create an Anonymous Variable by assigning a value to all writable properties of
the created instance. This is great in many scenarios, but not so much in others.
You can <a href="http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx">disable
this behavior by using the OmitAutoProperties method</a>, but sometimes, you <em>would</em> like
most of the writable properties set, except one or two.
</p>
        <p>
Consider this simple Person 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;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 Person\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf4 Person\cf0  spouse;\par ??\par ??    \cf1 public\cf0  \cf4 DateTime\cf0  BirthDay \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Name \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf4 Person\cf0  Spouse \par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .spouse; \}\par ??        \cf1 set\par ??\cf0         \{\par ??            \cf1 this\cf0 .spouse = \cf1 value\cf0 ;\par ??            \cf1 if\cf0  (\cf1 value\cf0  != \cf1 null\cf0 )\par ??            \{\par ??                \cf1 value\cf0 .spouse = \cf1 this\cf0 ;\par ??            \}\par ??        \}\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">Person</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: #2b91af">Person</span> spouse;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">DateTime</span> BirthDay
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">string</span> Name
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: #2b91af">Person</span> Spouse </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>.spouse;
}</pre>
          <pre style="margin: 0px">        <span style="color: blue">set</span></pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">this</span>.spouse
= <span style="color: blue">value</span>;</pre>
          <pre style="margin: 0px">            <span style="color: blue">if</span> (<span style="color: blue">value</span> != <span style="color: blue">null</span>)</pre>
          <pre style="margin: 0px">            {</pre>
          <pre style="margin: 0px">                <span style="color: blue">value</span>.spouse
= <span style="color: blue">this</span>;</pre>
          <pre style="margin: 0px">            }</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The main trouble with this class, seen from AutoFixture's perspective, is the circular
reference exposed by the Spouse property. When AutoFixture attempts to create an anonymous
instance of Person, it will create anonymous values for all writable properties, and
that includes the Spouse property, so it attempts to create a new instance of the
Person class and assign values to all public properties, including the Spouse property,
etc.
</p>
        <p>
In other words, this line of code throws a StackOverflowException:
</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  person = fixture.CreateAnonymous&lt;\cf4 Person\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> person
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Person</span>&gt;();</pre>
        </div>
        <p>
If you would still like to have anonymous values assigned to Name and BirthDay, you
can use the Without 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 var\cf0  person = fixture.Build&lt;\cf4 Person\cf0 &gt;()\par ??    .Without(p =&gt; p.Spouse)\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> person
= fixture.Build&lt;<span style="color: #2b91af">Person</span>&gt;()</pre>
          <pre style="margin: 0px">    .Without(p =&gt; p.Spouse)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
This will give you an anonymous instance of the Person class, but with the Spouse
property still with its default value of null.
</p>
        <p>
Several calls to Without can be chained if you want to skip more than one property.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0fd21249-f8d1-4095-b428-42db301f63cc" />
      </body>
      <title>Omitting Only Certain Properties With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,0fd21249-f8d1-4095-b428-42db301f63cc.aspx</guid>
      <link>http://blog.ploeh.dk/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture.aspx</link>
      <pubDate>Mon, 17 Aug 2009 19:33:40 GMT</pubDate>
      <description>&lt;p&gt;
The default behavior of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; is
to create an Anonymous Variable by assigning a value to all writable properties of
the created instance. This is great in many scenarios, but not so much in others.
You can &lt;a href="http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx"&gt;disable
this behavior by using the OmitAutoProperties method&lt;/a&gt;, but sometimes, you &lt;em&gt;would&lt;/em&gt; like
most of the writable properties set, except one or two.
&lt;/p&gt;
&lt;p&gt;
Consider this simple Person 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;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 Person\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf4 Person\cf0  spouse;\par ??\par ??    \cf1 public\cf0  \cf4 DateTime\cf0  BirthDay \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Name \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf4 Person\cf0  Spouse \par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .spouse; \}\par ??        \cf1 set\par ??\cf0         \{\par ??            \cf1 this\cf0 .spouse = \cf1 value\cf0 ;\par ??            \cf1 if\cf0  (\cf1 value\cf0  != \cf1 null\cf0 )\par ??            \{\par ??                \cf1 value\cf0 .spouse = \cf1 this\cf0 ;\par ??            \}\par ??        \}\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;Person&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: #2b91af"&gt;Person&lt;/span&gt; spouse;&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: #2b91af"&gt;DateTime&lt;/span&gt; BirthDay
{ &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;&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;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;&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: #2b91af"&gt;Person&lt;/span&gt; Spouse &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;.spouse;
}&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;set&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;this&lt;/span&gt;.spouse
= &lt;span style="color: blue"&gt;value&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;if&lt;/span&gt; (&lt;span style="color: blue"&gt;value&lt;/span&gt; != &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;&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;value&lt;/span&gt;.spouse
= &lt;span style="color: blue"&gt;this&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;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The main trouble with this class, seen from AutoFixture's perspective, is the circular
reference exposed by the Spouse property. When AutoFixture attempts to create an anonymous
instance of Person, it will create anonymous values for all writable properties, and
that includes the Spouse property, so it attempts to create a new instance of the
Person class and assign values to all public properties, including the Spouse property,
etc.
&lt;/p&gt;
&lt;p&gt;
In other words, this line of code throws a StackOverflowException:
&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  person = fixture.CreateAnonymous&amp;lt;\cf4 Person\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; person
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Person&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If you would still like to have anonymous values assigned to Name and BirthDay, you
can use the Without 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 var\cf0  person = fixture.Build&amp;lt;\cf4 Person\cf0 &amp;gt;()\par ??    .Without(p =&amp;gt; p.Spouse)\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; person
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;Person&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Without(p =&amp;gt; p.Spouse)&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;
This will give you an anonymous instance of the Person class, but with the Spouse
property still with its default value of null.
&lt;/p&gt;
&lt;p&gt;
Several calls to Without can be chained if you want to skip more than one property.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0fd21249-f8d1-4095-b428-42db301f63cc" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,0fd21249-f8d1-4095-b428-42db301f63cc.aspx</comments>
      <category>AutoFixture</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=f96fb271-9bf0-4153-8c2e-f25245012892</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,f96fb271-9bf0-4153-8c2e-f25245012892.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,f96fb271-9bf0-4153-8c2e-f25245012892.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=f96fb271-9bf0-4153-8c2e-f25245012892</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A couple of days ago I released <a href="http://autofixture.codeplex.com/">AutoFixture</a> .8.4.
It contains a few small feature additions and a bug fix. You can get it at the <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29096">AutoFixture
CodePlex site</a> as usual.
</p>
        <p>
You may have noticed that I'm frequently releasing incremental versions these days.
This is because we have begun using AutoFixture for writing production software at <a href="http://www.safewhere.net/">Safewhere</a>.
So far, it has been a pleasure to use AutoFixture in my daily work, and watch colleagues
pick it up and run with it.
</p>
        <p>
Such intensive usage obviously uncovers missing features as well as a few bugs, which
is the driving force behind the frequent releases. I've been happy to observe that
so far, there have been only a few bugs, and the general API is very expressive and
useful.
</p>
        <p>
After we ship the first product written with AutoFixture, I plan to upgrade it to
version .9 (beta). That should hopefully happen in the autumn of 2009.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f96fb271-9bf0-4153-8c2e-f25245012892" />
      </body>
      <title>AutoFixture .8.4 And Roadmap</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,f96fb271-9bf0-4153-8c2e-f25245012892.aspx</guid>
      <link>http://blog.ploeh.dk/2009/07/11/AutoFixture84AndRoadmap.aspx</link>
      <pubDate>Sat, 11 Jul 2009 20:35:34 GMT</pubDate>
      <description>&lt;p&gt;
A couple of days ago I released &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; .8.4.
It contains a few small feature additions and a bug fix. You can get it at the &lt;a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29096"&gt;AutoFixture
CodePlex site&lt;/a&gt; as usual.
&lt;/p&gt;
&lt;p&gt;
You may have noticed that I'm frequently releasing incremental versions these days.
This is because we have begun using AutoFixture for writing production software at &lt;a href="http://www.safewhere.net/"&gt;Safewhere&lt;/a&gt;.
So far, it has been a pleasure to use AutoFixture in my daily work, and watch colleagues
pick it up and run with it.
&lt;/p&gt;
&lt;p&gt;
Such intensive usage obviously uncovers missing features as well as a few bugs, which
is the driving force behind the frequent releases. I've been happy to observe that
so far, there have been only a few bugs, and the general API is very expressive and
useful.
&lt;/p&gt;
&lt;p&gt;
After we ship the first product written with AutoFixture, I plan to upgrade it to
version .9 (beta). That should hopefully happen in the autumn of 2009.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f96fb271-9bf0-4153-8c2e-f25245012892" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,f96fb271-9bf0-4153-8c2e-f25245012892.aspx</comments>
      <category>AutoFixture</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=bc05b900-0198-4401-9281-0661cce23f47</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,bc05b900-0198-4401-9281-0661cce23f47.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,bc05b900-0198-4401-9281-0661cce23f47.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=bc05b900-0198-4401-9281-0661cce23f47</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It was only earlier this week that I released <a href="http://autofixture.codeplex.com/">AutoFixture</a> .8.2,
but now I'm releasing version .8.3 – not that there was anything wrong with .8.2 (that
I know of), but I had some time to implement new features, and I wanted to properly
release those.
</p>
        <p>
In the future I will blog about these new features (along with all the other AutoFixture
features I haven't introduced yet).
</p>
        <p>
Get it at the <a href="http://autofixture.codeplex.com/">AutoFixture CodePlex site</a> as
usual.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bc05b900-0198-4401-9281-0661cce23f47" />
      </body>
      <title>AutoFixture .8.3 Released</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,bc05b900-0198-4401-9281-0661cce23f47.aspx</guid>
      <link>http://blog.ploeh.dk/2009/06/20/AutoFixture83Released.aspx</link>
      <pubDate>Sat, 20 Jun 2009 08:34:30 GMT</pubDate>
      <description>&lt;p&gt;
It was only earlier this week that I released &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; .8.2,
but now I'm releasing version .8.3 – not that there was anything wrong with .8.2 (that
I know of), but I had some time to implement new features, and I wanted to properly
release those.
&lt;/p&gt;
&lt;p&gt;
In the future I will blog about these new features (along with all the other AutoFixture
features I haven't introduced yet).
&lt;/p&gt;
&lt;p&gt;
Get it at the &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture CodePlex site&lt;/a&gt; as
usual.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bc05b900-0198-4401-9281-0661cce23f47" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,bc05b900-0198-4401-9281-0661cce23f47.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=52f0daf5-7fa9-4469-9bd7-693cc6f013ba</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,52f0daf5-7fa9-4469-9bd7-693cc6f013ba.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,52f0daf5-7fa9-4469-9bd7-693cc6f013ba.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=52f0daf5-7fa9-4469-9bd7-693cc6f013ba</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Yesterday I created a new release (.8.2) of <a href="http://autofixture.codeplex.com/">AutoFixture</a>;
this time with a new feature that I recently discovered that I needed, and about which
I will blog later.
</p>
        <p>
There are no breaking changes and no known bugs.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=52f0daf5-7fa9-4469-9bd7-693cc6f013ba" />
      </body>
      <title>AutoFixture .8.2 Released</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,52f0daf5-7fa9-4469-9bd7-693cc6f013ba.aspx</guid>
      <link>http://blog.ploeh.dk/2009/06/18/AutoFixture82Released.aspx</link>
      <pubDate>Thu, 18 Jun 2009 19:11:39 GMT</pubDate>
      <description>&lt;p&gt;
Yesterday I created a new release (.8.2) of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;;
this time with a new feature that I recently discovered that I needed, and about which
I will blog later.
&lt;/p&gt;
&lt;p&gt;
There are no breaking changes and no known bugs.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=52f0daf5-7fa9-4469-9bd7-693cc6f013ba" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,52f0daf5-7fa9-4469-9bd7-693cc6f013ba.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=7ed1ecda-73ae-47a4-8be2-0bd276cbce50</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,7ed1ecda-73ae-47a4-8be2-0bd276cbce50.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,7ed1ecda-73ae-47a4-8be2-0bd276cbce50.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=7ed1ecda-73ae-47a4-8be2-0bd276cbce50</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Previously, we saw how you can <a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx">set
property values while building</a><a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">anonymous
variables</a> with <a href="http://autofixture.codeplex.com/">AutoFixture</a>. While
I insinuated that I consider this a somewhat atypical scenario, we can now safely
venture forth to the truly exotic :)
</p>
        <p>
Imagine that you want to build an anonymous instance of a type that requires you to
call a certain method before you can start assigning properties. It's difficult to
come up with a reasonable example of this, but although I consider it quite bad design,
I've seen interfaces that include an Initialize method that must be called before
any other member. In our example, let's call this interface IBadDesign.
</p>
        <p>
Let's say that we have a class called SomeImp that implements IBadDesign. Here's the
relevant part of the 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 #region\cf0  IBadDesign Members\par ??\par ??\cf1 public\cf0  \cf1 string\cf0  Message\par ??\{\par ??    \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .message; \}\par ??    \cf1 set\par ??\cf0     \{\par ??        \cf1 if\cf0  (\cf1 this\cf0 .mc == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 InvalidOperationException\cf0 (\cf5 "..."\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .message = \cf1 value\cf0 ;\par ??        \cf1 this\cf0 .transformedMessage = \cf1 this\cf0 .mc.DoStuff(\cf1 value\cf0 );\par ??    \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  Initialize(\cf4 MyClass\cf0  mc)\par ??\{\par ??    \cf1 this\cf0 .mc = mc;\par ??\}\par ??\par ??\cf1 #endregion}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">#region</span> IBadDesign
Members</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">string</span> Message</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>.message;
}</pre>
          <pre style="margin: 0px">    <span style="color: blue">set</span></pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (<span style="color: blue">this</span>.mc
== <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">InvalidOperationException</span>(<span style="color: #a31515">"..."</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.message
= <span style="color: blue">value</span>;</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.transformedMessage
= <span style="color: blue">this</span>.mc.DoStuff(<span style="color: blue">value</span>);</pre>
          <pre style="margin: 0px">    }</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> Initialize(<span style="color: #2b91af">MyClass</span> mc)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span>.mc
= mc;</pre>
          <pre style="margin: 0px">}</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: blue">#endregion</span>
          </pre>
        </div>
        <p>
This is a rather ridiculous example, but I couldn't think of a better one. The main
point here is that given a brand-new instance of SomeImp, this usage is invalid:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red163\green21\blue21;}??\fs20 something.Message = \cf3 "Ploeh"\cf0 ;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">something.Message = <span style="color: #a31515">"Ploeh"</span>;</pre>
        </div>
        <p>
While the above code snippet will throw an InvalidOperationException, this will work:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 something.Initialize(\cf3 new\cf0  \cf4 MyClass\cf0 ());\par ??something.Message = \cf5 "Ploeh"\cf0 ;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">something.Initialize(<span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>());</pre>
          <pre style="margin: 0px">something.Message = <span style="color: #a31515">"Ploeh"</span>;</pre>
        </div>
        <p>
The problem is that by default, AutoFixture ignores methods and only assigns properties,
which means that this is also going to throw:
</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  imp = fixture.CreateAnonymous&lt;\cf4 SomeImp\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> imp
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">SomeImp</span>&gt;();</pre>
        </div>
        <p>
As we saw with properties, we can use the Build method to customize how the type is
being created. Properties can be set with the With method, while methods can be invoked
with the Do method, so this is all it takes:
</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  imp = fixture.Build&lt;\cf4 SomeImp\cf0 &gt;()\par ??    .Do(s =&gt; s.Initialize(\cf1 new\cf0  \cf4 MyClass\cf0 ()))\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> imp
= fixture.Build&lt;<span style="color: #2b91af">SomeImp</span>&gt;()</pre>
          <pre style="margin: 0px">    .Do(s =&gt; s.Initialize(<span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>()))</pre>
          <pre style="margin: 0px">    .CreateAnonymous();    </pre>
        </div>
        <p>
We don't have to explicitly set the Message property, as AutoFixture is going to do
this automatically, and all implicit assignments happen after explicit actions defined
by With or Do (and in case you were wondering, you can mix With and Do, and ObjectBuilder&lt;T&gt;
will preserve the ordering).
</p>
        <p>
In most cases, having to use the Do method probably constitutes a design smell of
the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, but the method is there if
you need it.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7ed1ecda-73ae-47a4-8be2-0bd276cbce50" />
      </body>
      <title>Calling Methods While Building Anonymous Variables With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,7ed1ecda-73ae-47a4-8be2-0bd276cbce50.aspx</guid>
      <link>http://blog.ploeh.dk/2009/06/09/CallingMethodsWhileBuildingAnonymousVariablesWithAutoFixture.aspx</link>
      <pubDate>Tue, 09 Jun 2009 17:50:54 GMT</pubDate>
      <description>&lt;p&gt;
Previously, we saw how you can &lt;a href="http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx"&gt;set
property values while building&lt;/a&gt; &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;anonymous
variables&lt;/a&gt; with &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;. While
I insinuated that I consider this a somewhat atypical scenario, we can now safely
venture forth to the truly exotic :)
&lt;/p&gt;
&lt;p&gt;
Imagine that you want to build an anonymous instance of a type that requires you to
call a certain method before you can start assigning properties. It's difficult to
come up with a reasonable example of this, but although I consider it quite bad design,
I've seen interfaces that include an Initialize method that must be called before
any other member. In our example, let's call this interface IBadDesign.
&lt;/p&gt;
&lt;p&gt;
Let's say that we have a class called SomeImp that implements IBadDesign. Here's the
relevant part of the 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 #region\cf0  IBadDesign Members\par ??\par ??\cf1 public\cf0  \cf1 string\cf0  Message\par ??\{\par ??    \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .message; \}\par ??    \cf1 set\par ??\cf0     \{\par ??        \cf1 if\cf0  (\cf1 this\cf0 .mc == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 InvalidOperationException\cf0 (\cf5 "..."\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .message = \cf1 value\cf0 ;\par ??        \cf1 this\cf0 .transformedMessage = \cf1 this\cf0 .mc.DoStuff(\cf1 value\cf0 );\par ??    \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  Initialize(\cf4 MyClass\cf0  mc)\par ??\{\par ??    \cf1 this\cf0 .mc = mc;\par ??\}\par ??\par ??\cf1 #endregion}
--&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;#region&lt;/span&gt; IBadDesign
Members&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;public&lt;/span&gt; &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;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.message;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;set&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;if&lt;/span&gt; (&lt;span style="color: blue"&gt;this&lt;/span&gt;.mc
== &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;InvalidOperationException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"..."&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;.message
= &lt;span style="color: blue"&gt;value&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;.transformedMessage
= &lt;span style="color: blue"&gt;this&lt;/span&gt;.mc.DoStuff(&lt;span style="color: blue"&gt;value&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;}&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;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Initialize(&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; mc)&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;.mc
= mc;&lt;/pre&gt;&lt;pre style="margin: 0px"&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;#endregion&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This is a rather ridiculous example, but I couldn't think of a better one. The main
point here is that given a brand-new instance of SomeImp, this usage is invalid:
&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;\red163\green21\blue21;}??\fs20 something.Message = \cf3 "Ploeh"\cf0 ;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;something.Message = &lt;span style="color: #a31515"&gt;"Ploeh"&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
While the above code snippet will throw an InvalidOperationException, this will work:
&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;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 something.Initialize(\cf3 new\cf0  \cf4 MyClass\cf0 ());\par ??something.Message = \cf5 "Ploeh"\cf0 ;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;something.Initialize(&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;something.Message = &lt;span style="color: #a31515"&gt;"Ploeh"&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The problem is that by default, AutoFixture ignores methods and only assigns properties,
which means that this is also going to throw:
&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  imp = fixture.CreateAnonymous&amp;lt;\cf4 SomeImp\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; imp
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;SomeImp&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
As we saw with properties, we can use the Build method to customize how the type is
being created. Properties can be set with the With method, while methods can be invoked
with the Do method, so this is all it takes:
&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  imp = fixture.Build&amp;lt;\cf4 SomeImp\cf0 &amp;gt;()\par ??    .Do(s =&amp;gt; s.Initialize(\cf1 new\cf0  \cf4 MyClass\cf0 ()))\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; imp
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;SomeImp&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Do(s =&amp;gt; s.Initialize(&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; .CreateAnonymous();&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
We don't have to explicitly set the Message property, as AutoFixture is going to do
this automatically, and all implicit assignments happen after explicit actions defined
by With or Do (and in case you were wondering, you can mix With and Do, and ObjectBuilder&amp;lt;T&amp;gt;
will preserve the ordering).
&lt;/p&gt;
&lt;p&gt;
In most cases, having to use the Do method probably constitutes a design smell of
the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;, but the method is there if
you need it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7ed1ecda-73ae-47a4-8be2-0bd276cbce50" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,7ed1ecda-73ae-47a4-8be2-0bd276cbce50.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=54b09f14-e03f-4081-b493-5fe3f95cc47d</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,54b09f14-e03f-4081-b493-5fe3f95cc47d.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,54b09f14-e03f-4081-b493-5fe3f95cc47d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=54b09f14-e03f-4081-b493-5fe3f95cc47d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
To make it a bit easier to get started with <a href="http://autofixture.codeplex.com/">AutoFixture</a> without
having to trawl through all my blog posts, I've added a <a href="http://autofixture.codeplex.com/Wiki/View.aspx?title=CheatSheet">Cheat
Sheet</a> over at the AutoFixture CodePlex site.
</p>
        <p>
As I add more posts on AutoFixture, I'll update the Cheat Sheet with the essentials.
Please let me know if you think something's missing.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=54b09f14-e03f-4081-b493-5fe3f95cc47d" />
      </body>
      <title>AutoFixture Cheat Sheet</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,54b09f14-e03f-4081-b493-5fe3f95cc47d.aspx</guid>
      <link>http://blog.ploeh.dk/2009/06/04/AutoFixtureCheatSheet.aspx</link>
      <pubDate>Thu, 04 Jun 2009 21:15:08 GMT</pubDate>
      <description>&lt;p&gt;
To make it a bit easier to get started with &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; without
having to trawl through all my blog posts, I've added a &lt;a href="http://autofixture.codeplex.com/Wiki/View.aspx?title=CheatSheet"&gt;Cheat
Sheet&lt;/a&gt; over at the AutoFixture CodePlex site.
&lt;/p&gt;
&lt;p&gt;
As I add more posts on AutoFixture, I'll update the Cheat Sheet with the essentials.
Please let me know if you think something's missing.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=54b09f14-e03f-4081-b493-5fe3f95cc47d" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,54b09f14-e03f-4081-b493-5fe3f95cc47d.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=a5013fc1-44d8-4930-aa7d-77aee8d60db6</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,a5013fc1-44d8-4930-aa7d-77aee8d60db6.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,a5013fc1-44d8-4930-aa7d-77aee8d60db6.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=a5013fc1-44d8-4930-aa7d-77aee8d60db6</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my previous post I described <a href="http://blog.ploeh.dk/2009/05/26/TheAutoFixtureBuilder.aspx">how
the Build method can be used to customize how a single anonymous variable is created</a>.
</p>
        <p>
A common customization is to set a property value during creation. In most cases,
this can simply be done <em>after</em> the anonymous variable has been created (so
the following is not an AutoFixture customization):
</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  mc = fixture.CreateAnonymous&lt;\cf4 MyClass\cf0 &gt;();\par ??mc.MyText = \cf5 "Ploeh"\cf0 ;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">mc.MyText = <span style="color: #a31515">"Ploeh"</span>;</pre>
        </div>
        <p>
By default, <a href="http://autofixture.codeplex.com/">AutoFixture</a> assigns anonymous
values to all writable properties, but since they are writable, you can always explicitly
give them different values if you care.
</p>
        <p>
However, there are situations when a property is writable, but can't take just any
value of its type. Sometimes this is a sign that you should reconsider your API, as
I've <a href="http://blog.ploeh.dk/2009/05/01/DealingWithConstrainedInput.aspx">previously
described</a>, but it may also be a legitimate situation.
</p>
        <p>
Consider a Filter class that has Min and Max properties. To be semantically correct,
the Min property must be less than or equal to the Max property. Each property is
implemented 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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 int\cf0  Min\par ??\{\par ??    \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .min; \}\par ??    \cf1 set\par ??\cf0     \{\par ??        \cf1 if\cf0  (\cf1 value\cf0  &gt; \cf1 this\cf0 .Max)\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "value"\cf0 );\par ??        \}\par ??        \cf1 this\cf0 .min = \cf1 value\cf0 ;\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">int</span> Min</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>.min;
}</pre>
          <pre style="margin: 0px">    <span style="color: blue">set</span></pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (<span style="color: blue">value</span> &gt; <span style="color: blue">this</span>.Max)</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">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"value"</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.min
= <span style="color: blue">value</span>;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
When you ask AutoFixture to create an instance of the Filter class, it will throw
an exception because it's attempting to set the Min property after the Max property,
and the <a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx">default
algorithm for numbers</a> is to return numbers in an increasing sequence. (In this
example, the Min property is being assigned a value <em>after</em> the Max property,
but AutoFixture has no contract that states that the order in which properties are
assigned is guaranteed.) In other words, this throws an exception:
</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  f = fixture.CreateAnonymous&lt;\cf4 Filter\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> f
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">Filter</span>&gt;();</pre>
        </div>
        <p>
To solve this problem, we will have to customize the assignment of the Min and Max
properties, <em>before</em> we ask AutoFixture to create an instance of the Filter
class. Here's how to do that:
</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 int\cf0  min = fixture.CreateAnonymous&lt;\cf1 int\cf0 &gt;();\par ??\cf1 int\cf0  max = min + 1;\par ??\cf1 var\cf0  f = fixture.Build&lt;\cf4 Filter\cf0 &gt;()\par ??    .With(s =&gt; s.Max, max)\par ??    .With(s =&gt; s.Min, min)\par ??    .CreateAnonymous();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">int</span> min
= fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;();</pre>
          <pre style="margin: 0px">
            <span style="color: blue">int</span> max
= min + 1;</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> f =
fixture.Build&lt;<span style="color: #2b91af">Filter</span>&gt;()</pre>
          <pre style="margin: 0px">    .With(s =&gt; s.Max, max)</pre>
          <pre style="margin: 0px">    .With(s =&gt; s.Min, min)</pre>
          <pre style="margin: 0px">    .CreateAnonymous();</pre>
        </div>
        <p>
The With method lets you specify an expression that identifies a property, as well
as the value that should be assigned to that property. When you do that, AutoFixture
will never attempt to assign an anonymous value to that property, but will instead
use the value you specified.
</p>
        <p>
In most cases, just creating a truly anonymous instance and subsequently explicitly
assigning any significant values is easier, but using the Build method with one or
more calls to the With method gives you the power to override any property assignments
before the instance is created.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a5013fc1-44d8-4930-aa7d-77aee8d60db6" />
      </body>
      <title>Setting Property Values While Building Anonymous Variables With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,a5013fc1-44d8-4930-aa7d-77aee8d60db6.aspx</guid>
      <link>http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx</link>
      <pubDate>Mon, 01 Jun 2009 12:35:18 GMT</pubDate>
      <description>&lt;p&gt;
In my previous post I described &lt;a href="http://blog.ploeh.dk/2009/05/26/TheAutoFixtureBuilder.aspx"&gt;how
the Build method can be used to customize how a single anonymous variable is created&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
A common customization is to set a property value during creation. In most cases,
this can simply be done &lt;em&gt;after&lt;/em&gt; the anonymous variable has been created (so
the following is not an AutoFixture customization):
&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  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??mc.MyText = \cf5 "Ploeh"\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; mc
= 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;mc.MyText = &lt;span style="color: #a31515"&gt;"Ploeh"&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
By default, &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; assigns anonymous
values to all writable properties, but since they are writable, you can always explicitly
give them different values if you care.
&lt;/p&gt;
&lt;p&gt;
However, there are situations when a property is writable, but can't take just any
value of its type. Sometimes this is a sign that you should reconsider your API, as
I've &lt;a href="http://blog.ploeh.dk/2009/05/01/DealingWithConstrainedInput.aspx"&gt;previously
described&lt;/a&gt;, but it may also be a legitimate situation.
&lt;/p&gt;
&lt;p&gt;
Consider a Filter class that has Min and Max properties. To be semantically correct,
the Min property must be less than or equal to the Max property. Each property is
implemented 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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 int\cf0  Min\par ??\{\par ??    \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .min; \}\par ??    \cf1 set\par ??\cf0     \{\par ??        \cf1 if\cf0  (\cf1 value\cf0  &amp;gt; \cf1 this\cf0 .Max)\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "value"\cf0 );\par ??        \}\par ??        \cf1 this\cf0 .min = \cf1 value\cf0 ;\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;int&lt;/span&gt; Min&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;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.min;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;set&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;if&lt;/span&gt; (&lt;span style="color: blue"&gt;value&lt;/span&gt; &amp;gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Max)&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;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"value"&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;this&lt;/span&gt;.min
= &lt;span style="color: blue"&gt;value&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;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
When you ask AutoFixture to create an instance of the Filter class, it will throw
an exception because it's attempting to set the Min property after the Max property,
and the &lt;a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx"&gt;default
algorithm for numbers&lt;/a&gt; is to return numbers in an increasing sequence. (In this
example, the Min property is being assigned a value &lt;em&gt;after&lt;/em&gt; the Max property,
but AutoFixture has no contract that states that the order in which properties are
assigned is guaranteed.) In other words, this throws an exception:
&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  f = fixture.CreateAnonymous&amp;lt;\cf4 Filter\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; f
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;Filter&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
To solve this problem, we will have to customize the assignment of the Min and Max
properties, &lt;em&gt;before&lt;/em&gt; we ask AutoFixture to create an instance of the Filter
class. Here's how to do that:
&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 int\cf0  min = fixture.CreateAnonymous&amp;lt;\cf1 int\cf0 &amp;gt;();\par ??\cf1 int\cf0  max = min + 1;\par ??\cf1 var\cf0  f = fixture.Build&amp;lt;\cf4 Filter\cf0 &amp;gt;()\par ??    .With(s =&amp;gt; s.Max, max)\par ??    .With(s =&amp;gt; s.Min, min)\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;int&lt;/span&gt; min
= 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;&lt;span style="color: blue"&gt;int&lt;/span&gt; max
= min + 1;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; f =
fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;Filter&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(s =&amp;gt; s.Max, max)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .With(s =&amp;gt; s.Min, min)&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;
The With method lets you specify an expression that identifies a property, as well
as the value that should be assigned to that property. When you do that, AutoFixture
will never attempt to assign an anonymous value to that property, but will instead
use the value you specified.
&lt;/p&gt;
&lt;p&gt;
In most cases, just creating a truly anonymous instance and subsequently explicitly
assigning any significant values is easier, but using the Build method with one or
more calls to the With method gives you the power to override any property assignments
before the instance is created.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a5013fc1-44d8-4930-aa7d-77aee8d60db6" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,a5013fc1-44d8-4930-aa7d-77aee8d60db6.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Until now, I've shown you how you can make wholesale adjustments or customizations
to an entire Fixture instance, effectively changing the way it creates all instances
of a particular type.
</p>
        <p>
In some scenarios, you'd rather want to customize how a single instance is created
without influencing other instances of the same type. For this purpose, <a href="http://autofixture.codeplex.com/">AutoFixture</a> includes
a class called ObjectBuilder&lt;T&gt; that can be used to do exactly that. 
</p>
        <p>
The easiest way to get an instance of this class is by calling Build on a Fixture
instance. This will give you an instance of ObjectBuilder&lt;T&gt; that you can use
to customize the build steps. When you are done, CreateAnonymous returns the built
instance.
</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  mc = fixture.Build&lt;\cf4 MyClass\cf0 &gt;().CreateAnonymous();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mc
= fixture.Build&lt;<span style="color: #2b91af">MyClass</span>&gt;().CreateAnonymous();</pre>
        </div>
        <p>
This particular example doesn't define any customizations, so it's equivalent to
</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  mc = fixture.CreateAnonymous&lt;\cf4 MyClass\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> mc
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
        </div>
        <p>
In fact, Fixture.CreateAnonymous is little more than a convenience method wrapping
an ObjectBuilder (there's a few extra differences, but that's a topic for another
post).
</p>
        <p>
It's worth noting that the object specified by the type parameter to the Build method
is first created when you call CreateAnonymous.
</p>
        <p>
In future posts I'll demonstrate how to use the Build method to customize individual <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">anonymous
variables</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e" />
      </body>
      <title>The AutoFixture Builder</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e.aspx</guid>
      <link>http://blog.ploeh.dk/2009/05/26/TheAutoFixtureBuilder.aspx</link>
      <pubDate>Tue, 26 May 2009 21:30:35 GMT</pubDate>
      <description>&lt;p&gt;
Until now, I've shown you how you can make wholesale adjustments or customizations
to an entire Fixture instance, effectively changing the way it creates all instances
of a particular type.
&lt;/p&gt;
&lt;p&gt;
In some scenarios, you'd rather want to customize how a single instance is created
without influencing other instances of the same type. For this purpose, &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; includes
a class called ObjectBuilder&amp;lt;T&amp;gt; that can be used to do exactly that. 
&lt;/p&gt;
&lt;p&gt;
The easiest way to get an instance of this class is by calling Build on a Fixture
instance. This will give you an instance of ObjectBuilder&amp;lt;T&amp;gt; that you can use
to customize the build steps. When you are done, CreateAnonymous returns the built
instance.
&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  mc = fixture.Build&amp;lt;\cf4 MyClass\cf0 &amp;gt;().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; mc
= fixture.Build&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;().CreateAnonymous();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This particular example doesn't define any customizations, so it's equivalent to
&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  mc = fixture.CreateAnonymous&amp;lt;\cf4 MyClass\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; mc
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In fact, Fixture.CreateAnonymous is little more than a convenience method wrapping
an ObjectBuilder (there's a few extra differences, but that's a topic for another
post).
&lt;/p&gt;
&lt;p&gt;
It's worth noting that the object specified by the type parameter to the Build method
is first created when you call CreateAnonymous.
&lt;/p&gt;
&lt;p&gt;
In future posts I'll demonstrate how to use the Build method to customize individual &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;anonymous
variables&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,7c7377c0-0ffa-4e8f-a0e4-11b2d4cc549e.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1d8cf097-ca10-45d7-aac5-7cf0033f2756</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1d8cf097-ca10-45d7-aac5-7cf0033f2756.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1d8cf097-ca10-45d7-aac5-7cf0033f2756.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1d8cf097-ca10-45d7-aac5-7cf0033f2756</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I've created a new release (.8.1 for lack of a better version number) of <a href="http://autofixture.codeplex.com/">AutoFixture</a>.
While it contains some breaking changes, they all relate to features that I have yet
to cover here on the blog – in other words: All the examples that I've posted so far
are still valid.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1d8cf097-ca10-45d7-aac5-7cf0033f2756" />
      </body>
      <title>AutoFixture .8.1 Released</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1d8cf097-ca10-45d7-aac5-7cf0033f2756.aspx</guid>
      <link>http://blog.ploeh.dk/2009/05/17/AutoFixture81Released.aspx</link>
      <pubDate>Sun, 17 May 2009 07:48:36 GMT</pubDate>
      <description>&lt;p&gt;
Today I've created a new release (.8.1 for lack of a better version number) of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;.
While it contains some breaking changes, they all relate to features that I have yet
to cover here on the blog – in other words: All the examples that I've posted so far
are still valid.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1d8cf097-ca10-45d7-aac5-7cf0033f2756" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1d8cf097-ca10-45d7-aac5-7cf0033f2756.aspx</comments>
      <category>AutoFixture</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=d6d30aec-8f50-48e8-b36f-7c579637a114</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,d6d30aec-8f50-48e8-b36f-7c579637a114.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,d6d30aec-8f50-48e8-b36f-7c579637a114.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=d6d30aec-8f50-48e8-b36f-7c579637a114</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When writing unit tests you often need to deal with sequences and collections, populating
lists with anonymous data as part of setting up a <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>.
</p>
        <p>
This is easy to do with <a href="http://autofixture.codeplex.com/">AutoFixture</a>.
While you can obviously create a simple loop and call CreateAnonymous from within
the loop, AutoFixture provides some convenient methods for working with sequences.
</p>
        <p>
Equivalent to the CreateAnonymous method, the Fixture class also includes the CreateMany
method that creates a sequence of <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">anonymous
variables</a>. <em>CreateManyAnonymous</em> might have been a more concise and consistent
name for the method, but I felt that this was a bit too verbose.
</p>
        <p>
This will create an IEnumerable&lt;string&gt;:
</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;\red0\green0\blue255;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf4 var\cf0  strings = fixture.CreateMany&lt;\cf4 string\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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: blue">var</span> strings
= fixture.CreateMany&lt;<span style="color: blue">string</span>&gt;();</pre>
        </div>
        <p>
Obvously, you can create sequences of whatever type you want, as long as <a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx">AutoFixture
can figure out how to create instances of the type</a>:
</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  myInstances = fixture.CreateMany&lt;\cf4 MyClass\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> myInstances
= fixture.CreateMany&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
        </div>
        <p>
Being able to create sequences of anonymous data is nice, but sometimes you need to
add multiple anonymous items to an existing list (particularly if that list is a read-only
property of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>).
</p>
        <p>
To support that scenario, the Fixture class also has the AddManyTo method that can
be 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 var\cf0  list = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 MyClass\cf0 &gt;();\par ??fixture.AddManyTo(list);}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> list
= <span style="color: blue">new</span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">fixture.AddManyTo(list);</pre>
        </div>
        <p>
This simply creates many anonymous MyClass instances and adds them all to the list.
Once more, <em>AddManyAnonymousTo</em> might have been a more precise name, but again
I chose a less verbose alternative.
</p>
        <p>
If you want more control over how the instances are created, a more explicit overload
of AddManyTo gives you that.
</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  list = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf1 int\cf0 &gt;();\par ??\cf1 var\cf0  r = \cf1 new\cf0  \cf4 Random\cf0 ();\par ??fixture.AddManyTo(list, () =&gt; r.Next());}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> list
= <span style="color: blue">new</span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> r
= <span style="color: blue">new</span><span style="color: #2b91af">Random</span>();</pre>
          <pre style="margin: 0px">fixture.AddManyTo(list, () =&gt; r.Next());</pre>
        </div>
        <p>
The above examples adds many random numbers to the list of integers, since the second
parameters is a Func&lt;T&gt; used to create the instances.
</p>
        <p>
By default, these methods all create 3 anonymous variables when called, since <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3
is a good equivalent for <em>many</em></a>. If you want a different number of instances
to be created, you can modify the RepeatCount property.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.RepeatCount = 10;\par ??\cf3 var\cf0  sequence = fixture.CreateMany&lt;\cf4 MyClass\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">fixture.RepeatCount = 10;</pre>
          <pre style="margin: 0px">
            <span style="color: blue">var</span> sequence
= fixture.CreateMany&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
        </div>
        <p>
The above example will create an IEnumerable&lt;MyClass&gt; with 10 anonymous MyClass
instances, while this will add 7 anonymous instances to the list variable:
</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  list = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 MyClass\cf0 &gt;();\par ??fixture.RepeatCount = 7;\par ??fixture.AddManyTo(list);}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> list
= <span style="color: blue">new</span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">fixture.RepeatCount = 7;</pre>
          <pre style="margin: 0px">fixture.AddManyTo(list);</pre>
        </div>
        <p>
AutoFixture provides some convenient methods for creating and managing collections
of anonymous data. While it may seem simple (and it is), in a future post I will demonstrate
how it can save you quit a bit of infrastructure code, and enable you to write unit
tests that are shorter, more concise and more maintainable.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=d6d30aec-8f50-48e8-b36f-7c579637a114" />
      </body>
      <title>Anonymous Sequences With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,d6d30aec-8f50-48e8-b36f-7c579637a114.aspx</guid>
      <link>http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx</link>
      <pubDate>Mon, 11 May 2009 20:25:42 GMT</pubDate>
      <description>&lt;p&gt;
When writing unit tests you often need to deal with sequences and collections, populating
lists with anonymous data as part of setting up a &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
This is easy to do with &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;.
While you can obviously create a simple loop and call CreateAnonymous from within
the loop, AutoFixture provides some convenient methods for working with sequences.
&lt;/p&gt;
&lt;p&gt;
Equivalent to the CreateAnonymous method, the Fixture class also includes the CreateMany
method that creates a sequence of &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;anonymous
variables&lt;/a&gt;. &lt;em&gt;CreateManyAnonymous&lt;/em&gt; might have been a more concise and consistent
name for the method, but I felt that this was a bit too verbose.
&lt;/p&gt;
&lt;p&gt;
This will create an IEnumerable&amp;lt;string&amp;gt;:
&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;\red0\green0\blue255;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf4 var\cf0  strings = fixture.CreateMany&amp;lt;\cf4 string\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;&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;&lt;span style="color: blue"&gt;var&lt;/span&gt; strings
= fixture.CreateMany&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Obvously, you can create sequences of whatever type you want, as long as &lt;a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx"&gt;AutoFixture
can figure out how to create instances of the type&lt;/a&gt;:
&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  myInstances = fixture.CreateMany&amp;lt;\cf4 MyClass\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;&lt;span style="color: blue"&gt;var&lt;/span&gt; myInstances
= fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Being able to create sequences of anonymous data is nice, but sometimes you need to
add multiple anonymous items to an existing list (particularly if that list is a read-only
property of your &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
To support that scenario, the Fixture class also has the AddManyTo method that can
be 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 var\cf0  list = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??fixture.AddManyTo(list);}
--&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; list
= &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;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.AddManyTo(list);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This simply creates many anonymous MyClass instances and adds them all to the list.
Once more, &lt;em&gt;AddManyAnonymousTo&lt;/em&gt; might have been a more precise name, but again
I chose a less verbose alternative.
&lt;/p&gt;
&lt;p&gt;
If you want more control over how the instances are created, a more explicit overload
of AddManyTo gives you that.
&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  list = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf1 int\cf0 &amp;gt;();\par ??\cf1 var\cf0  r = \cf1 new\cf0  \cf4 Random\cf0 ();\par ??fixture.AddManyTo(list, () =&amp;gt; r.Next());}
--&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; list
= &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: blue"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; r
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Random&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.AddManyTo(list, () =&amp;gt; r.Next());&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The above examples adds many random numbers to the list of integers, since the second
parameters is a Func&amp;lt;T&amp;gt; used to create the instances.
&lt;/p&gt;
&lt;p&gt;
By default, these methods all create 3 anonymous variables when called, since &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;3
is a good equivalent for &lt;em&gt;many&lt;/em&gt;&lt;/a&gt;. If you want a different number of instances
to be created, you can modify the RepeatCount property.
&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;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.RepeatCount = 10;\par ??\cf3 var\cf0  sequence = fixture.CreateMany&amp;lt;\cf4 MyClass\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.RepeatCount = 10;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; sequence
= fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The above example will create an IEnumerable&amp;lt;MyClass&amp;gt; with 10 anonymous MyClass
instances, while this will add 7 anonymous instances to the list variable:
&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  list = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 MyClass\cf0 &amp;gt;();\par ??fixture.RepeatCount = 7;\par ??fixture.AddManyTo(list);}
--&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; list
= &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;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.RepeatCount = 7;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.AddManyTo(list);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
AutoFixture provides some convenient methods for creating and managing collections
of anonymous data. While it may seem simple (and it is), in a future post I will demonstrate
how it can save you quit a bit of infrastructure code, and enable you to write unit
tests that are shorter, more concise and more maintainable.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=d6d30aec-8f50-48e8-b36f-7c579637a114" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,d6d30aec-8f50-48e8-b36f-7c579637a114.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=91aa4494-6b37-4ac9-b268-11493706669f</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,91aa4494-6b37-4ac9-b268-11493706669f.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,91aa4494-6b37-4ac9-b268-11493706669f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=91aa4494-6b37-4ac9-b268-11493706669f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As a response to my description of <a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx">how
AutoFixture creates objects</a>, Klaus asked:
</p>
        <p>
“[What] if the constructor of ComplexChild imposes some kind of restriction on its
parameter? If, for example, instead of the "name" parameter, it would take a "phoneNumber"
parameter (as a string), and do some format checking?”
</p>
        <p>
Now that we have covered some of the basic features of <a href="http://autofixture.codeplex.com/">AutoFixture</a>,
it’s time to properly answer this excellent question.
</p>
        <p>
For simplicity’s sake, let’s assume that the phone number in question is a Danish
phone number: This is pretty good for example code, since a Danish phone number is
essentially just an 8-digit number. It can have white space and an optional country
code (+45), but strip that away, and it’s just an 8-digit number. However, there are
exceptions, since the emergency number is 112 (equivalent to the American 911), and
other 3-digit special numbers exist as well.
</p>
        <p>
With that in mind, let’s look at a simple Contact class that contains a contact’s
name and Danish phone number. The constructor might look 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  Contact(\cf1 string\cf0  name, \cf1 string\cf0  phoneNumber)\par ??\{\par ??    \cf1 this\cf0 .Name = name;\par ??    \cf1 this\cf0 .PhoneNumber = \par ??        \cf4 Contact\cf0 .ParsePhoneNumber(phoneNumber);\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> Contact(<span style="color: blue">string</span> name, <span style="color: blue">string</span> phoneNumber)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span>.Name
= name;</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span>.PhoneNumber
= </pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Contact</span>.ParsePhoneNumber(phoneNumber);</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The static ParsePhoneNumber method strips away white space and optional country code
and parses the normalized string to a number. This fits the scenario laid out in Klaus’
question.
</p>
        <p>
So what happens when we ask AutoFixture to create an instance of Contact? It will
Reflect over Contact’s constructor and create two new anonymous string instances –
one for name, and one for phoneNumber. As <a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx">previously
described</a>, each string will be created as a Guid prepended with a named hint –
in this case the argument name. Thus, the phoneNumber argument will get a value like
"phoneNumberfa432351-1563-4769-842c-7588af32a056", which will cause the ParsePhoneNumber
method to throw an exception.
</p>
        <p>
How do we deal with that?
</p>
        <p>
The most obvious fix is to <a href="http://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms.aspx">modify
AutoFixture’s algorithm for generating strings</a>. Here 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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 fixture.Register&lt;\cf3 string\cf0 &gt;(() =&gt; \cf4 "112"\cf0 );}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: blue">string</span>&gt;(()
=&gt; <span style="color: #a31515">"112"</span>);</pre>
        </div>
        <p>
This will simply cause <em>all</em> generated strings to be "112", including the Contact
instance's Name property. In unit testing, this may not be a problem in itself, since,
from an API perspective, the name could in principle be any string.
</p>
        <p>
However, if the Contact class also had an Email property that was parsed and verified
from a string argument, we'd be in trouble, since "112" is not a valid email address.
</p>
        <p>
We can't easily modify the string generation algorithm to fit the requirements for
both a Danish telephone number and an email address.
</p>
        <p>
Should we then conclude that AutoFixture isn't really useful after all?
</p>
        <p>
On the contrary, this is a hint to us that the Contact class' API could be better.
If an automated tool can't figure out how to generate correct input, how can we expect
other developers to do it?
</p>
        <p>
Although humans can make leaps of intuition, an API should still go to great lengths
to protect its users from making mistakes. Asking for an unbounded string and then
expecting it to be in a particular format may not always be the best option available.
</p>
        <p>
In our particular case, the Value Object pattern offers a better alternative. Our
first version of the DanishPhoneNumber class simply takes an integer as a constructor
argument:
</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  DanishPhoneNumber(\cf1 int\cf0  number)\par ??\{\par ??    \cf1 this\cf0 .number = number;\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> DanishPhoneNumber(<span style="color: blue">int</span> number)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span>.number
= number;</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
If we still need to parse strings (e.g. from user input), we could add a static Parse,
or even a TryParse, method and test that method in isolation without involving the
Contact class.
</p>
        <p>
This neatly solves our original issue with AutoFixture, since it will now create a
new instance of DanishPhoneNumber as part of the creation process when we ask for
an anonymous Contact instance.
</p>
        <p>
The only remaining issue is that by default, the number fed into the DanishPhoneNumber
instance is likely to be considerably less than 112 – actually, <a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx">if
no other Int32 instances are created, it will be <em>1</em></a>.
</p>
        <p>
This will be a problem if we modify the DanishPhoneNumber constructor to look 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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  DanishPhoneNumber(\cf1 int\cf0  number)\par ??\{\par ??    \cf1 if\cf0  ((number &lt; 112) ||\par ??        (number &gt; 99999999))\par ??    \{\par ??        \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "number"\cf0 );\par ??    \}\par ??    \cf1 this\cf0 .number = number;\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> DanishPhoneNumber(<span style="color: blue">int</span> number)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">if</span> ((number
&lt; 112) ||</pre>
          <pre style="margin: 0px">        (number &gt; 99999999))</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">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"number"</span>);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">    <span style="color: blue">this</span>.number
= number;</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Unless a unit test has already caused AutFixture to previously create 111 other integers
(highly unlikely), CreateAnonymous&lt;Contact&gt; is going to throw an exception.
</p>
        <p>
This is easy to fix. Once again, the most obvious fix is to modify the creation algorithm
for integers.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 fixture.Register&lt;\cf3 int\cf0 &gt;(() =&gt; 12345678);}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: blue">int</span>&gt;(()
=&gt; 12345678);</pre>
        </div>
        <p>
However, this will cause that particular instance of Fixture to return 12345678 <em>every</em> time
you ask it to create an anonymous integer. Depending on the scenario, this may or
may not be a problem.
</p>
        <p>
A more targeted solution is to <a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx">specifically
address the algorithm</a> for generating DanishPhoneNumber instances:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.Register&lt;\cf3 int\cf0 , \cf4 DanishPhoneNumber\cf0 &gt;(i =&gt; \par ??    \cf3 new\cf0  \cf4 DanishPhoneNumber\cf0 (i + 112));}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: blue">int</span>, <span style="color: #2b91af">DanishPhoneNumber</span>&gt;(i
=&gt; </pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">DanishPhoneNumber</span>(i
+ 112));</pre>
        </div>
        <p>
Here, I've even used the Register overload that automatically provides an anonymous
integer to feed into the DanishPhoneNumber constructor, so all I have to do is ensure
that the number falls into the proper range. Adding 112 (the minimum) neatly does
the trick.
</p>
        <p>
If you don't like the hard-coded value of 112 in the test, you can use that to further
drive the design. In this case, we can add a MinValue to DanishPhoneNumber:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.Register&lt;\cf3 int\cf0 , \cf4 DanishPhoneNumber\cf0 &gt;(i =&gt;\par ??    \cf3 new\cf0  \cf4 DanishPhoneNumber\cf0 (i + \par ??        \cf4 DanishPhoneNumber\cf0 .MinValue));}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: blue">int</span>, <span style="color: #2b91af">DanishPhoneNumber</span>&gt;(i
=&gt;</pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">DanishPhoneNumber</span>(i
+ </pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">DanishPhoneNumber</span>.MinValue));</pre>
        </div>
        <p>
Obvously, MinValue will also be used in DanishPhoneNumber's constructor to define
the lower limit of the Guard Clause.
</p>
        <p>
In my opinion, a good API should guide the user and make it difficult to make mistakes.
In many ways, you can view AutoFixture as an exceptionally dim user of your API. This
is the reason I really enjoyed receiving Klaus' original question: Like other TDD
practices, AutoFixture drives better design.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=91aa4494-6b37-4ac9-b268-11493706669f" />
      </body>
      <title>Dealing With Constrained Input</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,91aa4494-6b37-4ac9-b268-11493706669f.aspx</guid>
      <link>http://blog.ploeh.dk/2009/05/01/DealingWithConstrainedInput.aspx</link>
      <pubDate>Fri, 01 May 2009 03:56:00 GMT</pubDate>
      <description>&lt;p&gt;
As a response to my description of &lt;a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx"&gt;how
AutoFixture creates objects&lt;/a&gt;, Klaus asked:
&lt;/p&gt;
&lt;p&gt;
“[What] if the constructor of ComplexChild imposes some kind of restriction on its
parameter? If, for example, instead of the "name" parameter, it would take a "phoneNumber"
parameter (as a string), and do some format checking?”
&lt;/p&gt;
&lt;p&gt;
Now that we have covered some of the basic features of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;,
it’s time to properly answer this excellent question.
&lt;/p&gt;
&lt;p&gt;
For simplicity’s sake, let’s assume that the phone number in question is a Danish
phone number: This is pretty good for example code, since a Danish phone number is
essentially just an 8-digit number. It can have white space and an optional country
code (+45), but strip that away, and it’s just an 8-digit number. However, there are
exceptions, since the emergency number is 112 (equivalent to the American 911), and
other 3-digit special numbers exist as well.
&lt;/p&gt;
&lt;p&gt;
With that in mind, let’s look at a simple Contact class that contains a contact’s
name and Danish phone number. The constructor might look 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  Contact(\cf1 string\cf0  name, \cf1 string\cf0  phoneNumber)\par ??\{\par ??    \cf1 this\cf0 .Name = name;\par ??    \cf1 this\cf0 .PhoneNumber = \par ??        \cf4 Contact\cf0 .ParsePhoneNumber(phoneNumber);\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; Contact(&lt;span style="color: blue"&gt;string&lt;/span&gt; name, &lt;span style="color: blue"&gt;string&lt;/span&gt; phoneNumber)&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;.Name
= name;&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;.PhoneNumber
= &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;Contact&lt;/span&gt;.ParsePhoneNumber(phoneNumber);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The static ParsePhoneNumber method strips away white space and optional country code
and parses the normalized string to a number. This fits the scenario laid out in Klaus’
question.
&lt;/p&gt;
&lt;p&gt;
So what happens when we ask AutoFixture to create an instance of Contact? It will
Reflect over Contact’s constructor and create two new anonymous string instances –
one for name, and one for phoneNumber. As &lt;a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx"&gt;previously
described&lt;/a&gt;, each string will be created as a Guid prepended with a named hint –
in this case the argument name. Thus, the phoneNumber argument will get a value like
"phoneNumberfa432351-1563-4769-842c-7588af32a056", which will cause the ParsePhoneNumber
method to throw an exception.
&lt;/p&gt;
&lt;p&gt;
How do we deal with that?
&lt;/p&gt;
&lt;p&gt;
The most obvious fix is to &lt;a href="http://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms.aspx"&gt;modify
AutoFixture’s algorithm for generating strings&lt;/a&gt;. Here 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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 fixture.Register&amp;lt;\cf3 string\cf0 &amp;gt;(() =&amp;gt; \cf4 "112"\cf0 );}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(()
=&amp;gt; &lt;span style="color: #a31515"&gt;"112"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This will simply cause &lt;em&gt;all&lt;/em&gt; generated strings to be "112", including the Contact
instance's Name property. In unit testing, this may not be a problem in itself, since,
from an API perspective, the name could in principle be any string.
&lt;/p&gt;
&lt;p&gt;
However, if the Contact class also had an Email property that was parsed and verified
from a string argument, we'd be in trouble, since "112" is not a valid email address.
&lt;/p&gt;
&lt;p&gt;
We can't easily modify the string generation algorithm to fit the requirements for
both a Danish telephone number and an email address.
&lt;/p&gt;
&lt;p&gt;
Should we then conclude that AutoFixture isn't really useful after all?
&lt;/p&gt;
&lt;p&gt;
On the contrary, this is a hint to us that the Contact class' API could be better.
If an automated tool can't figure out how to generate correct input, how can we expect
other developers to do it?
&lt;/p&gt;
&lt;p&gt;
Although humans can make leaps of intuition, an API should still go to great lengths
to protect its users from making mistakes. Asking for an unbounded string and then
expecting it to be in a particular format may not always be the best option available.
&lt;/p&gt;
&lt;p&gt;
In our particular case, the Value Object pattern offers a better alternative. Our
first version of the DanishPhoneNumber class simply takes an integer as a constructor
argument:
&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  DanishPhoneNumber(\cf1 int\cf0  number)\par ??\{\par ??    \cf1 this\cf0 .number = number;\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; DanishPhoneNumber(&lt;span style="color: blue"&gt;int&lt;/span&gt; number)&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;.number
= number;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If we still need to parse strings (e.g. from user input), we could add a static Parse,
or even a TryParse, method and test that method in isolation without involving the
Contact class.
&lt;/p&gt;
&lt;p&gt;
This neatly solves our original issue with AutoFixture, since it will now create a
new instance of DanishPhoneNumber as part of the creation process when we ask for
an anonymous Contact instance.
&lt;/p&gt;
&lt;p&gt;
The only remaining issue is that by default, the number fed into the DanishPhoneNumber
instance is likely to be considerably less than 112 – actually, &lt;a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx"&gt;if
no other Int32 instances are created, it will be &lt;em&gt;1&lt;/em&gt;&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
This will be a problem if we modify the DanishPhoneNumber constructor to look 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;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  DanishPhoneNumber(\cf1 int\cf0  number)\par ??\{\par ??    \cf1 if\cf0  ((number &amp;lt; 112) ||\par ??        (number &amp;gt; 99999999))\par ??    \{\par ??        \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentOutOfRangeException\cf0 (\cf5 "number"\cf0 );\par ??    \}\par ??    \cf1 this\cf0 .number = number;\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; DanishPhoneNumber(&lt;span style="color: blue"&gt;int&lt;/span&gt; number)&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;if&lt;/span&gt; ((number
&amp;lt; 112) ||&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (number &amp;gt; 99999999))&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;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"number"&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: blue"&gt;this&lt;/span&gt;.number
= number;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Unless a unit test has already caused AutFixture to previously create 111 other integers
(highly unlikely), CreateAnonymous&amp;lt;Contact&amp;gt; is going to throw an exception.
&lt;/p&gt;
&lt;p&gt;
This is easy to fix. Once again, the most obvious fix is to modify the creation algorithm
for integers.
&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;\red0\green0\blue255;}??\fs20 fixture.Register&amp;lt;\cf3 int\cf0 &amp;gt;(() =&amp;gt; 12345678);}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(()
=&amp;gt; 12345678);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
However, this will cause that particular instance of Fixture to return 12345678 &lt;em&gt;every&lt;/em&gt; time
you ask it to create an anonymous integer. Depending on the scenario, this may or
may not be a problem.
&lt;/p&gt;
&lt;p&gt;
A more targeted solution is to &lt;a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx"&gt;specifically
address the algorithm&lt;/a&gt; for generating DanishPhoneNumber instances:
&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;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.Register&amp;lt;\cf3 int\cf0 , \cf4 DanishPhoneNumber\cf0 &amp;gt;(i =&amp;gt; \par ??    \cf3 new\cf0  \cf4 DanishPhoneNumber\cf0 (i + 112));}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;DanishPhoneNumber&lt;/span&gt;&amp;gt;(i
=&amp;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;DanishPhoneNumber&lt;/span&gt;(i
+ 112));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Here, I've even used the Register overload that automatically provides an anonymous
integer to feed into the DanishPhoneNumber constructor, so all I have to do is ensure
that the number falls into the proper range. Adding 112 (the minimum) neatly does
the trick.
&lt;/p&gt;
&lt;p&gt;
If you don't like the hard-coded value of 112 in the test, you can use that to further
drive the design. In this case, we can add a MinValue to DanishPhoneNumber:
&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;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.Register&amp;lt;\cf3 int\cf0 , \cf4 DanishPhoneNumber\cf0 &amp;gt;(i =&amp;gt;\par ??    \cf3 new\cf0  \cf4 DanishPhoneNumber\cf0 (i + \par ??        \cf4 DanishPhoneNumber\cf0 .MinValue));}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;DanishPhoneNumber&lt;/span&gt;&amp;gt;(i
=&amp;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;DanishPhoneNumber&lt;/span&gt;(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;span style="color: #2b91af"&gt;DanishPhoneNumber&lt;/span&gt;.MinValue));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Obvously, MinValue will also be used in DanishPhoneNumber's constructor to define
the lower limit of the Guard Clause.
&lt;/p&gt;
&lt;p&gt;
In my opinion, a good API should guide the user and make it difficult to make mistakes.
In many ways, you can view AutoFixture as an exceptionally dim user of your API. This
is the reason I really enjoyed receiving Klaus' original question: Like other TDD
practices, AutoFixture drives better design.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=91aa4494-6b37-4ac9-b268-11493706669f" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,91aa4494-6b37-4ac9-b268-11493706669f.aspx</comments>
      <category>AutoFixture</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1c32432b-9806-435f-9f95-accf7ec84337</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1c32432b-9806-435f-9f95-accf7ec84337.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1c32432b-9806-435f-9f95-accf7ec84337.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1c32432b-9806-435f-9f95-accf7ec84337</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Several times in my previous <a href="http://autofixture.codeplex.com/">AutoFixture</a> posts,
I’ve insinuated that you can change the algorithms used for creating strings, numbers
and so on, if you don’t like the defaults.
</p>
        <p>
One way you can do this is by simply using the <a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx">Register
method that I introduced in my previous post</a>. Let’s say that you want to replace
the string algorithm to simply return a specific string:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 fixture.Register&lt;\cf3 string\cf0 &gt;(() =&gt; \cf4 "ploeh"\cf0 );}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: blue">string</span>&gt;(()
=&gt; <span style="color: #a31515">"ploeh"</span>);</pre>
        </div>
        <p>
No matter how many times you’ll call CreateAnonymous&lt;string&gt; on that particular
fixture object, it will always return <em>ploeh</em>.
</p>
        <p>
The Register method is really only a type-safe convenience method that wraps access
to the TypeMappings property. TypeMappings is just a Dictionary of types mapped to
functions. By default, the Fixture class has a set of pre-defined TypeMappings for
primitive types such as strings, numbers and booleans, so you could access the function
used to generate strings by indexing into this Dictionary with the System.String type.
</p>
        <p>
Equivalent to the above example, you could alternatively replace the string algorithm
like this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 fixture.TypeMappings[\cf3 typeof\cf0 (\cf3 string\cf0 )] = s =&gt; \cf4 "fnaah"\cf0 ;}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.TypeMappings[<span style="color: blue">typeof</span>(<span style="color: blue">string</span>)]
= s =&gt; <span style="color: #a31515">"fnaah"</span>;</pre>
        </div>
        <p>
Instead of using the Register method, I here assign a lambda expression directly to
the key identified by the System.String type. This is what the Register method does,
so the result is exactly the same.
</p>
        <p>
However, you may have noticed that by accessing TypeMappings directly, the signature
of the function is different. The Register method takes a Func&lt;T&gt;, whereas the
TypeMappings Dictionary expects a Func&lt;object, object&gt;. As you can see, the
Register method is more type-safe, but the TypeMappings Dictionary gives you a chance
to utilize the optional seed that one of the CreateAnonymous overloads takes.
</p>
        <p>
You could, for example, do this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.TypeMappings[\cf3 typeof\cf0 (\cf3 string\cf0 )] = s =&gt;\par ??    \cf3 string\cf0 .Format((\cf3 string\cf0 )s, \cf3 new\cf0  \cf4 Random\cf0 ().Next(100));}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.TypeMappings[<span style="color: blue">typeof</span>(<span style="color: blue">string</span>)]
= s =&gt;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span>.Format((<span style="color: blue">string</span>)s, <span style="color: blue">new</span><span style="color: #2b91af">Random</span>().Next(100));</pre>
        </div>
        <p>
Although this particular algorithm has a built-in weakness (can you spot it?), we
can now use the seed to provide a format string, 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;\red163\green21\blue21;}??\fs20 \cf1 string\cf0  result = fixture.CreateAnonymous(\cf4 "Risk: \{0\}%"\cf0 );}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">string</span> result
= fixture.CreateAnonymous(<span style="color: #a31515">"Risk: {0}%"</span>);</pre>
        </div>
        <p>
which will yield a result like <em>Risk: 32%</em>.
</p>
        <p>
When I designed the extensibility mechanism for AutoFixture, I seriously considered
defining an interface that all TypeMappings had to implement, but I ended up preferring
a Func&lt;object, object&gt; instead, since this allows you to redefine a particular
algorithm inline in a test by using an anonymous delegate or lambda expression, and
you can also reuse an existing algorithm, as long as it fits the signature.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1c32432b-9806-435f-9f95-accf7ec84337" />
      </body>
      <title>Replacing AutoFixture’s Default Algorithms</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1c32432b-9806-435f-9f95-accf7ec84337.aspx</guid>
      <link>http://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms.aspx</link>
      <pubDate>Mon, 27 Apr 2009 17:42:07 GMT</pubDate>
      <description>&lt;p&gt;
Several times in my previous &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; posts,
I’ve insinuated that you can change the algorithms used for creating strings, numbers
and so on, if you don’t like the defaults.
&lt;/p&gt;
&lt;p&gt;
One way you can do this is by simply using the &lt;a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx"&gt;Register
method that I introduced in my previous post&lt;/a&gt;. Let’s say that you want to replace
the string algorithm to simply return a specific string:
&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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 fixture.Register&amp;lt;\cf3 string\cf0 &amp;gt;(() =&amp;gt; \cf4 "ploeh"\cf0 );}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(()
=&amp;gt; &lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
No matter how many times you’ll call CreateAnonymous&amp;lt;string&amp;gt; on that particular
fixture object, it will always return &lt;em&gt;ploeh&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
The Register method is really only a type-safe convenience method that wraps access
to the TypeMappings property. TypeMappings is just a Dictionary of types mapped to
functions. By default, the Fixture class has a set of pre-defined TypeMappings for
primitive types such as strings, numbers and booleans, so you could access the function
used to generate strings by indexing into this Dictionary with the System.String type.
&lt;/p&gt;
&lt;p&gt;
Equivalent to the above example, you could alternatively replace the string algorithm
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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 fixture.TypeMappings[\cf3 typeof\cf0 (\cf3 string\cf0 )] = s =&amp;gt; \cf4 "fnaah"\cf0 ;}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.TypeMappings[&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)]
= s =&amp;gt; &lt;span style="color: #a31515"&gt;"fnaah"&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Instead of using the Register method, I here assign a lambda expression directly to
the key identified by the System.String type. This is what the Register method does,
so the result is exactly the same.
&lt;/p&gt;
&lt;p&gt;
However, you may have noticed that by accessing TypeMappings directly, the signature
of the function is different. The Register method takes a Func&amp;lt;T&amp;gt;, whereas the
TypeMappings Dictionary expects a Func&amp;lt;object, object&amp;gt;. As you can see, the
Register method is more type-safe, but the TypeMappings Dictionary gives you a chance
to utilize the optional seed that one of the CreateAnonymous overloads takes.
&lt;/p&gt;
&lt;p&gt;
You could, for example, do 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;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 fixture.TypeMappings[\cf3 typeof\cf0 (\cf3 string\cf0 )] = s =&amp;gt;\par ??    \cf3 string\cf0 .Format((\cf3 string\cf0 )s, \cf3 new\cf0  \cf4 Random\cf0 ().Next(100));}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.TypeMappings[&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)]
= s =&amp;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;.Format((&lt;span style="color: blue"&gt;string&lt;/span&gt;)s, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Random&lt;/span&gt;().Next(100));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Although this particular algorithm has a built-in weakness (can you spot it?), we
can now use the seed to provide a format string, 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;\red163\green21\blue21;}??\fs20 \cf1 string\cf0  result = fixture.CreateAnonymous(\cf4 "Risk: \{0\}%"\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;string&lt;/span&gt; result
= fixture.CreateAnonymous(&lt;span style="color: #a31515"&gt;"Risk: {0}%"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
which will yield a result like &lt;em&gt;Risk: 32%&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
When I designed the extensibility mechanism for AutoFixture, I seriously considered
defining an interface that all TypeMappings had to implement, but I ended up preferring
a Func&amp;lt;object, object&amp;gt; instead, since this allows you to redefine a particular
algorithm inline in a test by using an anonymous delegate or lambda expression, and
you can also reuse an existing algorithm, as long as it fits the signature.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1c32432b-9806-435f-9f95-accf7ec84337" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1c32432b-9806-435f-9f95-accf7ec84337.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=757f4b71-bad7-4651-abb8-83579ac575a4</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,757f4b71-bad7-4651-abb8-83579ac575a4.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,757f4b71-bad7-4651-abb8-83579ac575a4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=757f4b71-bad7-4651-abb8-83579ac575a4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Now that I’ve described how <a href="http://autofixture.codeplex.com/">AutoFixture</a><a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx">creates
objects</a>, it’s time to look at a bit more advanced scenario. As you may recall,
AutoFixture creates an object graph based on the public constructors of the types
contained in that object graph.
</p>
        <p>
That’s all well and good when all involved types have public constructors, but what
happens when this is not the case?
</p>
        <p>
Imagine that the MyClass constructor has this signature:
</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  mi)}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> MyClass(<span style="color: #2b91af">IMyInterface</span> mi)</pre>
        </div>
        <p>
Since IMyInterface is an interface it has no public constructors, so this will not
work:
</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;\red0\green0\blue255;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf1 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf1 MyClass\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
        </div>
        <p>
The second line of code will throw an <strike>ArgumentException</strike> ObjectCreationException*
stating that “AutoFixture was unable to create an instance of type Ploeh.QualityTools.AutoFixtureDocumentationTest.Intermediate.IMyInterface,
since it has no public constructor.” Not terribly surprising, actually.
</p>
        <p>
To resolve this issue, the Register method allows you to specify a custom function
that creates an object of the requested type. In the case of IMyInterface, that would
be a Func&lt;IMyInterface&gt;:
</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;\red0\green0\blue255;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??fixture.Register&lt;\cf1 IMyInterface\cf0 &gt;(() =&gt; \par ??    \cf4 new\cf0  \cf1 FakeMyInterface\cf0 ());\par ??\cf1 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf1 MyClass\cf0 &gt;();}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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">fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(()
=&gt; </pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>());</pre>
          <pre style="margin: 0px">
            <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
        </div>
        <p>
Here, I use a lambda expression to register the FakeMyInterface type, so that every
time that particular Fixture instance is asked to create an instance of IMyInterface,
it will invoke the lambda expression, and thus return an instance of FakeMyInterface
(which obviously implements IMyInterface).
</p>
        <p>
The Register method simply lets you map types without public constructors to concrete
types created by a function you specify.
</p>
        <p>
A more advanced scenario arises if you wish to use a specific text with this FakeMyInterface
constructor overload:
</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  FakeMyInterface(\cf1 int\cf0  number, \cf1 string\cf0  text)}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> FakeMyInterface(<span style="color: blue">int</span> number, <span style="color: blue">string</span> text)</pre>
        </div>
        <p>
Obviously, you can do it manually like this:
</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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf4 int\cf0  anonymousNumber = fixture.CreateAnonymous&lt;\cf4 int\cf0 &gt;();\par ??\cf4 string\cf0  knownText = \cf5 "This text is not anonymous"\cf0 ;\par ??fixture.Register&lt;\cf1 IMyInterface\cf0 &gt;(() =&gt; \par ??    \cf4 new\cf0  \cf1 FakeMyInterface\cf0 (anonymousNumber, knownText));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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: blue">int</span> anonymousNumber
= fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;();</pre>
          <pre style="margin: 0px">
            <span style="color: blue">string</span> knownText
= <span style="color: #a31515">"This text is not anonymous"</span>;</pre>
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(()
=&gt; </pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>(anonymousNumber,
knownText));</pre>
        </div>
        <p>
Here, I simply use the fixture object to create an anonymous number, while the knownText
variable is explicitly assigned a value, and then both are used as outer variables
in the Register function.
</p>
        <p>
This is, however, a common scenario, so the Register method has some convenience overloads
that will supply anonymous input parameters for you to use or throw away as you like.
This means that I can rewrite the above example to this:
</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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf4 string\cf0  knownText = \cf5 "This text is not anonymous"\cf0 ;\par ??fixture.Register&lt;\cf4 int\cf0 , \cf4 string\cf0 , \cf1 IMyInterface\cf0 &gt;((i, s) =&gt; \par ??    \cf4 new\cf0  \cf1 FakeMyInterface\cf0 (i, knownText));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <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: blue">string</span> knownText
= <span style="color: #a31515">"This text is not anonymous"</span>;</pre>
          <pre style="margin: 0px">fixture.Register&lt;<span style="color: blue">int</span>, <span style="color: blue">string</span>, <span style="color: #2b91af">IMyInterface</span>&gt;((i,
s) =&gt; </pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>(i,
knownText));</pre>
        </div>
        <p>
Compared to the previous example, I save a line of code. The drawback is that I have
to explicitly specify the type parameters to the Register method. In my book, that’s
a pretty good tradeoff, as it removes a line of irrelevant code, and allows the test
to focus on the relevant parts.
</p>
        <p>
Notice that this Register overload creates both an anonymous integer and an anonymous
string, but since I don’t want to use the anonymous string (s), I just ignore it and
use the knownText variable instead.
</p>
        <p>
          <strong>*Edit (2009-09-02): </strong>Changed the name of the Exception type to correctly
reflect a breaking change introduced in <a href="http://blog.ploeh.dk/2009/09/02/AutoFixture85Released.aspx">AutoFixture
.8.5</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=757f4b71-bad7-4651-abb8-83579ac575a4" />
      </body>
      <title>Dealing With Types Without Public Constructors</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,757f4b71-bad7-4651-abb8-83579ac575a4.aspx</guid>
      <link>http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx</link>
      <pubDate>Thu, 23 Apr 2009 19:51:42 GMT</pubDate>
      <description>&lt;p&gt;
Now that I’ve described how &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; &lt;a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx"&gt;creates
objects&lt;/a&gt;, it’s time to look at a bit more advanced scenario. As you may recall,
AutoFixture creates an object graph based on the public constructors of the types
contained in that object graph.
&lt;/p&gt;
&lt;p&gt;
That’s all well and good when all involved types have public constructors, but what
happens when this is not the case?
&lt;/p&gt;
&lt;p&gt;
Imagine that the MyClass constructor has this signature:
&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  mi)}
--&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; MyClass(&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; mi)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Since IMyInterface is an interface it has no public constructors, so this will not
work:
&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;\red0\green0\blue255;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf1 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf1 MyClass\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;&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;&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;/div&gt;
&lt;p&gt;
The second line of code will throw an &lt;strike&gt;ArgumentException&lt;/strike&gt; ObjectCreationException*
stating that “AutoFixture was unable to create an instance of type Ploeh.QualityTools.AutoFixtureDocumentationTest.Intermediate.IMyInterface,
since it has no public constructor.” Not terribly surprising, actually.
&lt;/p&gt;
&lt;p&gt;
To resolve this issue, the Register method allows you to specify a custom function
that creates an object of the requested type. In the case of IMyInterface, that would
be a Func&amp;lt;IMyInterface&amp;gt;:
&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;\red0\green0\blue255;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??fixture.Register&amp;lt;\cf1 IMyInterface\cf0 &amp;gt;(() =&amp;gt; \par ??    \cf4 new\cf0  \cf1 FakeMyInterface\cf0 ());\par ??\cf1 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf1 MyClass\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;&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;fixture.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; &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;&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;/div&gt;
&lt;p&gt;
Here, I use a lambda expression to register the FakeMyInterface type, so that every
time that particular Fixture instance is asked to create an instance of IMyInterface,
it will invoke the lambda expression, and thus return an instance of FakeMyInterface
(which obviously implements IMyInterface).
&lt;/p&gt;
&lt;p&gt;
The Register method simply lets you map types without public constructors to concrete
types created by a function you specify.
&lt;/p&gt;
&lt;p&gt;
A more advanced scenario arises if you wish to use a specific text with this FakeMyInterface
constructor overload:
&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  FakeMyInterface(\cf1 int\cf0  number, \cf1 string\cf0  text)}
--&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; FakeMyInterface(&lt;span style="color: blue"&gt;int&lt;/span&gt; number, &lt;span style="color: blue"&gt;string&lt;/span&gt; text)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Obviously, you can do it manually like this:
&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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf4 int\cf0  anonymousNumber = fixture.CreateAnonymous&amp;lt;\cf4 int\cf0 &amp;gt;();\par ??\cf4 string\cf0  knownText = \cf5 "This text is not anonymous"\cf0 ;\par ??fixture.Register&amp;lt;\cf1 IMyInterface\cf0 &amp;gt;(() =&amp;gt; \par ??    \cf4 new\cf0  \cf1 FakeMyInterface\cf0 (anonymousNumber, knownText));}
--&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;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;&lt;span style="color: blue"&gt;int&lt;/span&gt; anonymousNumber
= 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;&lt;span style="color: blue"&gt;string&lt;/span&gt; knownText
= &lt;span style="color: #a31515"&gt;"This text is not anonymous"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.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; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;(anonymousNumber,
knownText));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Here, I simply use the fixture object to create an anonymous number, while the knownText
variable is explicitly assigned a value, and then both are used as outer variables
in the Register function.
&lt;/p&gt;
&lt;p&gt;
This is, however, a common scenario, so the Register method has some convenience overloads
that will supply anonymous input parameters for you to use or throw away as you like.
This means that I can rewrite the above example to this:
&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;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Fixture\cf0  fixture = \cf4 new\cf0  \cf1 Fixture\cf0 ();\par ??\cf4 string\cf0  knownText = \cf5 "This text is not anonymous"\cf0 ;\par ??fixture.Register&amp;lt;\cf4 int\cf0 , \cf4 string\cf0 , \cf1 IMyInterface\cf0 &amp;gt;((i, s) =&amp;gt; \par ??    \cf4 new\cf0  \cf1 FakeMyInterface\cf0 (i, knownText));}
--&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;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;&lt;span style="color: blue"&gt;string&lt;/span&gt; knownText
= &lt;span style="color: #a31515"&gt;"This text is not anonymous"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;fixture.Register&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt;&amp;gt;((i,
s) =&amp;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;FakeMyInterface&lt;/span&gt;(i,
knownText));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Compared to the previous example, I save a line of code. The drawback is that I have
to explicitly specify the type parameters to the Register method. In my book, that’s
a pretty good tradeoff, as it removes a line of irrelevant code, and allows the test
to focus on the relevant parts.
&lt;/p&gt;
&lt;p&gt;
Notice that this Register overload creates both an anonymous integer and an anonymous
string, but since I don’t want to use the anonymous string (s), I just ignore it and
use the knownText variable instead.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;*Edit (2009-09-02): &lt;/strong&gt;Changed the name of the Exception type to correctly
reflect a breaking change introduced in &lt;a href="http://blog.ploeh.dk/2009/09/02/AutoFixture85Released.aspx"&gt;AutoFixture
.8.5&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=757f4b71-bad7-4651-abb8-83579ac575a4" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,757f4b71-bad7-4651-abb8-83579ac575a4.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=dedc6a2d-08a9-4d74-a05a-fbddd3bddb29</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,dedc6a2d-08a9-4d74-a05a-fbddd3bddb29.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,dedc6a2d-08a9-4d74-a05a-fbddd3bddb29.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=dedc6a2d-08a9-4d74-a05a-fbddd3bddb29</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
(Just back after 14 days in Morocco, I'll pick up where I left…)
</p>
        <p>
The last group of built-in special creation algorithms for <a href="http://autofixture.codeplex.com/">AutoFixture</a>,
besides <a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx">strings</a> and <a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx">numbers</a>,
concerns Booleans.
</p>
        <p>
The default algorithm is to alternate between true and false, starting with true;
i.e. the first time you invoke
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous&lt;\cf3 bool\cf0 &gt;()}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.CreateAnonymous&lt;<span style="color: blue">bool</span>&gt;();</pre>
        </div>
        <p>
it will return <em>true</em>, the next time <em>false</em>, then <em>true</em> again,
etc.
</p>
        <p>
The reason I chose this algorithm was because I wanted to ensure that the first time
AutoFixture creates an anonymous Boolean, it will return true, which is different
than the default (false, in case you were in doubt). This gives us better assurance
that a given constructor argument or property is being assigned a real value instead
of the default.
</p>
        <p>
Like with numbers, using the overload that takes a seed has no effect, and the seed
is simply ignored.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous(\cf3 true\cf0 )}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.CreateAnonymous(<span style="color: blue">true</span>);</pre>
        </div>
        <p>
In other words, the above method is still going to return <em>false</em> every second
time, so it doesn’t really make sense to use this overload at all for Booleans (or
numbers).
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=dedc6a2d-08a9-4d74-a05a-fbddd3bddb29" />
      </body>
      <title>Creating Booleans With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,dedc6a2d-08a9-4d74-a05a-fbddd3bddb29.aspx</guid>
      <link>http://blog.ploeh.dk/2009/04/19/CreatingBooleansWithAutoFixture.aspx</link>
      <pubDate>Sun, 19 Apr 2009 17:40:36 GMT</pubDate>
      <description>&lt;p&gt;
(Just back after 14 days in Morocco, I'll pick up where I left…)
&lt;/p&gt;
&lt;p&gt;
The last group of built-in special creation algorithms for &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;,
besides &lt;a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx"&gt;strings&lt;/a&gt; and &lt;a href="http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx"&gt;numbers&lt;/a&gt;,
concerns Booleans.
&lt;/p&gt;
&lt;p&gt;
The default algorithm is to alternate between true and false, starting with true;
i.e. the first time you invoke
&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;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous&amp;lt;\cf3 bool\cf0 &amp;gt;()}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.CreateAnonymous&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
it will return &lt;em&gt;true&lt;/em&gt;, the next time &lt;em&gt;false&lt;/em&gt;, then &lt;em&gt;true&lt;/em&gt; again,
etc.
&lt;/p&gt;
&lt;p&gt;
The reason I chose this algorithm was because I wanted to ensure that the first time
AutoFixture creates an anonymous Boolean, it will return true, which is different
than the default (false, in case you were in doubt). This gives us better assurance
that a given constructor argument or property is being assigned a real value instead
of the default.
&lt;/p&gt;
&lt;p&gt;
Like with numbers, using the overload that takes a seed has no effect, and the seed
is simply ignored.
&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;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous(\cf3 true\cf0 )}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.CreateAnonymous(&lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In other words, the above method is still going to return &lt;em&gt;false&lt;/em&gt; every second
time, so it doesn’t really make sense to use this overload at all for Booleans (or
numbers).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=dedc6a2d-08a9-4d74-a05a-fbddd3bddb29" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,dedc6a2d-08a9-4d74-a05a-fbddd3bddb29.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=0660df80-1827-416f-9aa3-8842aab14693</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,0660df80-1827-416f-9aa3-8842aab14693.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,0660df80-1827-416f-9aa3-8842aab14693.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=0660df80-1827-416f-9aa3-8842aab14693</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx">Previously</a>,
we saw how <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates strings.
In this post, I’ll explain how it creates numbers. Once again, the algorithm that
I’ll explain here is the default algorithm, but if you don’t like it, you can replace
it with something else.
</p>
        <p>
It’s very simple: Numbers are returned in the ordered sequence of natural numbers
(1, 2, 3, 4, 5, …). The first time you call
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous&lt;\cf3 int\cf0 &gt;();}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;();</pre>
        </div>
        <p>
the returned number will be <em>1</em>, the second time <em>2</em>, etc.
</p>
        <p>
The reason I chose that particular algorithm is because it creates numbers that we,
as humans, find… well… natural!
</p>
        <p>
A lot of the domains we model work with natural numbers, and even if you write an
API where negative numbers are allowed, it’s fairly unlikely that positive numbers
will <em>not</em> be allowed. Thus, in most cases, small positive integers tend to
be ‘nice’ values in most APIs – and recall that when we do TDD, we focus on the Happy
Path, so it’s important to pick values that take us down that path.
</p>
        <p>
Using the overload that takes a seed, like this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;}??\fs20 fixture.CreateAnonymous(42);}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.CreateAnonymous(42);</pre>
        </div>
        <p>
has no effect – the seed (in this case <em>42</em>) is simply ignored, so if you call
this after first calling the parameterless overload twice, the return number is going
to be <em>3</em>.
</p>
        <p>
Each number type, however, has its own sequence, so even if you’ve been creating a
few Int32 instances like above,
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous&lt;\cf3 decimal\cf0 &gt;();}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">fixture.CreateAnonymous&lt;<span style="color: blue">decimal</span>&gt;();</pre>
        </div>
        <p>
will return <em>1</em>.
</p>
        <p>
The following number types all work that way:
</p>
        <ul>
          <li>
Byte 
</li>
          <li>
Decimal 
</li>
          <li>
Double 
</li>
          <li>
Int16 
</li>
          <li>
Int32 
</li>
          <li>
Int64 
</li>
          <li>
SByte 
</li>
          <li>
Single 
</li>
          <li>
UInt16 
</li>
          <li>
UInt32 
</li>
          <li>
UInt64</li>
        </ul>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0660df80-1827-416f-9aa3-8842aab14693" />
      </body>
      <title>Creating Numbers With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,0660df80-1827-416f-9aa3-8842aab14693.aspx</guid>
      <link>http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx</link>
      <pubDate>Fri, 03 Apr 2009 21:07:13 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx"&gt;Previously&lt;/a&gt;,
we saw how &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; creates strings.
In this post, I’ll explain how it creates numbers. Once again, the algorithm that
I’ll explain here is the default algorithm, but if you don’t like it, you can replace
it with something else.
&lt;/p&gt;
&lt;p&gt;
It’s very simple: Numbers are returned in the ordered sequence of natural numbers
(1, 2, 3, 4, 5, …). The first time you call
&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;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous&amp;lt;\cf3 int\cf0 &amp;gt;();}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.CreateAnonymous&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
the returned number will be &lt;em&gt;1&lt;/em&gt;, the second time &lt;em&gt;2&lt;/em&gt;, etc.
&lt;/p&gt;
&lt;p&gt;
The reason I chose that particular algorithm is because it creates numbers that we,
as humans, find… well… natural!
&lt;/p&gt;
&lt;p&gt;
A lot of the domains we model work with natural numbers, and even if you write an
API where negative numbers are allowed, it’s fairly unlikely that positive numbers
will &lt;em&gt;not&lt;/em&gt; be allowed. Thus, in most cases, small positive integers tend to
be ‘nice’ values in most APIs – and recall that when we do TDD, we focus on the Happy
Path, so it’s important to pick values that take us down that path.
&lt;/p&gt;
&lt;p&gt;
Using the overload that takes a seed, 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;}??\fs20 fixture.CreateAnonymous(42);}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.CreateAnonymous(42);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
has no effect – the seed (in this case &lt;em&gt;42&lt;/em&gt;) is simply ignored, so if you call
this after first calling the parameterless overload twice, the return number is going
to be &lt;em&gt;3&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
Each number type, however, has its own sequence, so even if you’ve been creating a
few Int32 instances like above,
&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;\red0\green0\blue255;}??\fs20 fixture.CreateAnonymous&amp;lt;\cf3 decimal\cf0 &amp;gt;();}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;fixture.CreateAnonymous&amp;lt;&lt;span style="color: blue"&gt;decimal&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
will return &lt;em&gt;1&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
The following number types all work that way:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Byte 
&lt;li&gt;
Decimal 
&lt;li&gt;
Double 
&lt;li&gt;
Int16 
&lt;li&gt;
Int32 
&lt;li&gt;
Int64 
&lt;li&gt;
SByte 
&lt;li&gt;
Single 
&lt;li&gt;
UInt16 
&lt;li&gt;
UInt32 
&lt;li&gt;
UInt64&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0660df80-1827-416f-9aa3-8842aab14693" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,0660df80-1827-416f-9aa3-8842aab14693.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=8db71bbc-ea3b-40d2-b221-61d8af278d66</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,8db71bbc-ea3b-40d2-b221-61d8af278d66.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,8db71bbc-ea3b-40d2-b221-61d8af278d66.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=8db71bbc-ea3b-40d2-b221-61d8af278d66</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As <a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx">previously
hinted</a>, <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates primitive
types like strings, numbers, etc. using special algorithms.
</p>
        <p>
In this post, I’ll describe the <em>default</em> algorithm for strings. If you don’t
like this particular algorithm, you can replace it with your own.
</p>
        <p>
If you don’t care about the created string at all, you can just create it 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 string\cf0  anonymousText = \par ??    fixture.CreateAnonymous&lt;\cf1 string\cf0 &gt;();}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">string</span> anonymousText
= </pre>
          <pre style="margin: 0px">    fixture.CreateAnonymous&lt;<span style="color: blue">string</span>&gt;();</pre>
        </div>
        <p>
The algorithm is simply to create a new Guid and convert it to a string, so that anonymousText
will have a value like “f5cdf6b1-a473-410f-95f3-f427f7abb0c7”. Obviously, you don’t
know exactly which value will be returned, but that’s the whole point of <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a>.
</p>
        <p>
When I create string values as <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Explicit
Expectations</a>, I prefer that the Assert failure message contains some sort of hint
for me, so I can instead provide a hint to the CreateAnonymous 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;\red163\green21\blue21;}??\fs20 \cf1 string\cf0  anonymousName = fixture.CreateAnonymous(\cf4 "Name"\cf0 );}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">string</span> anonymousName
= fixture.CreateAnonymous(<span style="color: #a31515">"Name"</span>);</pre>
        </div>
        <p>
This overload is still generic, but since I provide a string as input, type inferencing
takes care of the rest.
</p>
        <p>
This is still going to create a Guid, but will now prepend the hint, giving a string
like “Name30a35da1-d681-441b-9db3-77ff51728b58”.
</p>
        <p>
Now, when my test fails, I’ll get an error message equivalent to
</p>
        <p>
“Assert.AreEqual failed. Expected:&lt;Namef2b1f55b-e9dc-4aac-a1ab-128dc80d3b71&gt;.
Actual:&lt;ploeh&gt;. Boo hiss”
</p>
        <p>
which I find marginally more informative than if the hint hadn’t been there.
</p>
        <p>
In a future post, I’ll explain how you can replace this algorithm with something else.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=8db71bbc-ea3b-40d2-b221-61d8af278d66" />
      </body>
      <title>Creating Strings With AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,8db71bbc-ea3b-40d2-b221-61d8af278d66.aspx</guid>
      <link>http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx</link>
      <pubDate>Thu, 02 Apr 2009 05:29:35 GMT</pubDate>
      <description>&lt;p&gt;
As &lt;a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx"&gt;previously
hinted&lt;/a&gt;, &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; creates primitive
types like strings, numbers, etc. using special algorithms.
&lt;/p&gt;
&lt;p&gt;
In this post, I’ll describe the &lt;em&gt;default&lt;/em&gt; algorithm for strings. If you don’t
like this particular algorithm, you can replace it with your own.
&lt;/p&gt;
&lt;p&gt;
If you don’t care about the created string at all, you can just create it 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 string\cf0  anonymousText = \par ??    fixture.CreateAnonymous&amp;lt;\cf1 string\cf0 &amp;gt;();}
--&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;string&lt;/span&gt; anonymousText
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The algorithm is simply to create a new Guid and convert it to a string, so that anonymousText
will have a value like “f5cdf6b1-a473-410f-95f3-f427f7abb0c7”. Obviously, you don’t
know exactly which value will be returned, but that’s the whole point of &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
When I create string values as &lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Explicit
Expectations&lt;/a&gt;, I prefer that the Assert failure message contains some sort of hint
for me, so I can instead provide a hint to the CreateAnonymous 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;\red163\green21\blue21;}??\fs20 \cf1 string\cf0  anonymousName = fixture.CreateAnonymous(\cf4 "Name"\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;string&lt;/span&gt; anonymousName
= fixture.CreateAnonymous(&lt;span style="color: #a31515"&gt;"Name"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This overload is still generic, but since I provide a string as input, type inferencing
takes care of the rest.
&lt;/p&gt;
&lt;p&gt;
This is still going to create a Guid, but will now prepend the hint, giving a string
like “Name30a35da1-d681-441b-9db3-77ff51728b58”.
&lt;/p&gt;
&lt;p&gt;
Now, when my test fails, I’ll get an error message equivalent to
&lt;/p&gt;
&lt;p&gt;
“Assert.AreEqual failed. Expected:&amp;lt;Namef2b1f55b-e9dc-4aac-a1ab-128dc80d3b71&amp;gt;.
Actual:&amp;lt;ploeh&amp;gt;. Boo hiss”
&lt;/p&gt;
&lt;p&gt;
which I find marginally more informative than if the hint hadn’t been there.
&lt;/p&gt;
&lt;p&gt;
In a future post, I’ll explain how you can replace this algorithm with something else.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=8db71bbc-ea3b-40d2-b221-61d8af278d66" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,8db71bbc-ea3b-40d2-b221-61d8af278d66.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=3d4824e9-4772-4266-825f-1e2dce8fdf13</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3d4824e9-4772-4266-825f-1e2dce8fdf13.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3d4824e9-4772-4266-825f-1e2dce8fdf13.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3d4824e9-4772-4266-825f-1e2dce8fdf13</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a>, but you’d probably like to know <em>how</em> it does it. This post
explains how.
</p>
        <p>
As we <a href="http://blog.ploeh.dk/2009/03/22/AnnouncingAutoFixture.aspx">previously
saw</a>, the CreateAnonymous method can create a new instance of a type known to it
only from its type parameter:
</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 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf1 MyClass\cf0 &gt;();}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
        </div>
        <p>
AutoFixture was never compiled with any knowledge of the MyClass type, so it obviously
uses Reflection to create the instance. That’s hardly surprising in itself.
</p>
        <p>
In the case of MyClass, it has a default constructor, so creating an instance is as
simple as it can be, but what happens if we instead ask for a more complex instance?
</p>
        <p>
As an example, the ComplexParent type has this constructor:
</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  ComplexParent(\cf4 ComplexChild\cf0  child)}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> ComplexParent(<span style="color: #2b91af">ComplexChild</span> child)</pre>
        </div>
        <p>
ComplexChild, however, has two constructors:
</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  ComplexChild(\cf1 string\cf0  name)}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> ComplexChild(<span style="color: blue">string</span> name)</pre>
        </div>
        <p>
and
</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  ComplexChild(\cf1 string\cf0  name, \cf1 int\cf0  number)}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> ComplexChild(<span style="color: blue">string</span> name, <span style="color: blue">int</span> number)</pre>
        </div>
        <p>
So what happens when we ask AutoFixture to create an instance of ComplexParent?
</p>
        <p>
ComplexParent only has a single public constructor, so AutoFixture doesn’t have any
other choice than picking that. This means that it must now create an anonymous instance
of ComplexChild.
</p>
        <p>
Fortunately, AutoFixture’s raison d’être is creating objects, so creating an instance
of ComplexChild isn’t a big deal; the only thing it needs to figure out is which constructor
to pick. When multiple public constructors are available, it always picks the one
with the fewest number of arguments – in this case ComplexChild(string).
</p>
        <p>
Obviously, it then needs to create an anonymous string value. For primitive types
like strings, numbers and booleans, AutoFixture has custom algorithms for value creation.
Since I’ll cover those mechanisms later, suffice it to say that <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a> is used to create an anonymous string.
</p>
        <p>
At this point, AutoFixture has all the information it needs, and it can now return
a properly initialized instance of ComplexParent.
</p>
        <p>
This ability to create instances of almost arbitrarily complex types is a real time-saver:
That, more than the ability to create single strings or numbers, was the reason I
originally created AutoFixture, since I got tired of initializing complex object graphs
just to satisfy some API that the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Test
Fixture</a> requires.
</p>
        <p>
It also has the additional advantage that it hides all the irrelevant object creation
code that the Test Fixture needs, but which isn’t relevant for the test at hand.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3d4824e9-4772-4266-825f-1e2dce8fdf13" />
      </body>
      <title>How AutoFixture Creates Objects</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3d4824e9-4772-4266-825f-1e2dce8fdf13.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx</link>
      <pubDate>Tue, 24 Mar 2009 20:22:49 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; creates &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt;, but you’d probably like to know &lt;em&gt;how&lt;/em&gt; it does it. This post
explains how.
&lt;/p&gt;
&lt;p&gt;
As we &lt;a href="http://blog.ploeh.dk/2009/03/22/AnnouncingAutoFixture.aspx"&gt;previously
saw&lt;/a&gt;, the CreateAnonymous method can create a new instance of a type known to it
only from its type parameter:
&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 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf1 MyClass\cf0 &amp;gt;();}
--&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;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;/div&gt;
&lt;p&gt;
AutoFixture was never compiled with any knowledge of the MyClass type, so it obviously
uses Reflection to create the instance. That’s hardly surprising in itself.
&lt;/p&gt;
&lt;p&gt;
In the case of MyClass, it has a default constructor, so creating an instance is as
simple as it can be, but what happens if we instead ask for a more complex instance?
&lt;/p&gt;
&lt;p&gt;
As an example, the ComplexParent type has this constructor:
&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  ComplexParent(\cf4 ComplexChild\cf0  child)}
--&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; ComplexParent(&lt;span style="color: #2b91af"&gt;ComplexChild&lt;/span&gt; child)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
ComplexChild, however, has two constructors:
&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  ComplexChild(\cf1 string\cf0  name)}
--&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; ComplexChild(&lt;span style="color: blue"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
and
&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  ComplexChild(\cf1 string\cf0  name, \cf1 int\cf0  number)}
--&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; ComplexChild(&lt;span style="color: blue"&gt;string&lt;/span&gt; name, &lt;span style="color: blue"&gt;int&lt;/span&gt; number)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
So what happens when we ask AutoFixture to create an instance of ComplexParent?
&lt;/p&gt;
&lt;p&gt;
ComplexParent only has a single public constructor, so AutoFixture doesn’t have any
other choice than picking that. This means that it must now create an anonymous instance
of ComplexChild.
&lt;/p&gt;
&lt;p&gt;
Fortunately, AutoFixture’s raison d’être is creating objects, so creating an instance
of ComplexChild isn’t a big deal; the only thing it needs to figure out is which constructor
to pick. When multiple public constructors are available, it always picks the one
with the fewest number of arguments – in this case ComplexChild(string).
&lt;/p&gt;
&lt;p&gt;
Obviously, it then needs to create an anonymous string value. For primitive types
like strings, numbers and booleans, AutoFixture has custom algorithms for value creation.
Since I’ll cover those mechanisms later, suffice it to say that &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt; is used to create an anonymous string.
&lt;/p&gt;
&lt;p&gt;
At this point, AutoFixture has all the information it needs, and it can now return
a properly initialized instance of ComplexParent.
&lt;/p&gt;
&lt;p&gt;
This ability to create instances of almost arbitrarily complex types is a real time-saver:
That, more than the ability to create single strings or numbers, was the reason I
originally created AutoFixture, since I got tired of initializing complex object graphs
just to satisfy some API that the &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Test
Fixture&lt;/a&gt; requires.
&lt;/p&gt;
&lt;p&gt;
It also has the additional advantage that it hides all the irrelevant object creation
code that the Test Fixture needs, but which isn’t relevant for the test at hand.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3d4824e9-4772-4266-825f-1e2dce8fdf13" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3d4824e9-4772-4266-825f-1e2dce8fdf13.aspx</comments>
      <category>AutoFixture</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>
  </channel>
</rss>