<commit_before>---
layout: post
tags: [AutoFixture, Unit Testing]
date: 2010-10-08 08:48:07 UTC
title: "AutoData Theories with AutoFixture"
---
{% include JB/setup %}

<div id="post">
	<p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0 comes with a new extension for <a href="http://xunit.codeplex.com/">xUnit.net</a> <a href="http://blog.benhall.me.uk/2008/01/introduction-to-xunitnet-extensions.html">data theories</a>. For those of us using xUnit.net, it can help make our unit tests more succinct and declarative.</p> <blockquote> <p>AutoFixture's support for xUnit.net is implemented in a separate assembly. AutoFixture itself has no dependency on xUnit.net, and if you use another unit testing framework, you can just ignore the existence of the Ploeh.AutoFixture.Xunit assembly.</p></blockquote> <p>Let's go back and revisit <a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx">the previous test</a> we wrote using AutoFixture and its auto-mocking extension:</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">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>());</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <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">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; sut.Add(pizza);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>Notice how all of the Fixture Setup phase is only used to create various objects that will be used in the test. First we create the fixture object, and then we use it to create four other objects. That turns out to be a pretty common idiom when using AutoFixture, so it's worthwhile to reduce the clutter if possible.</p>
<p>With xUnit.net's excellent extensibility features, we can. AutoFixture 2.0 now includes the AutoDataAttribute in a separate assembly. AutoDataAttribute derives from xUnit.net's DataAttribute (just like InlineDataAttribute), and while we can use it as is, it becomes really powerful if we combine it with auto-mocking 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  \cf1 class\cf0  \cf4 AutoMoqDataAttribute\cf0  : \cf4 AutoDataAttribute\par ??\cf0 \{\par ??    \cf1 public\cf0  AutoMoqDataAttribute()\par ??        : \cf1 base\cf0 (\cf1 new\cf0  \cf4 Fixture\cf0 ()\par ??            .Customize(\cf1 new\cf0  \cf4 AutoMoqCustomization\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">AutoMoqDataAttribute</span> : <span style="color: #2b91af">AutoDataAttribute</span></pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> AutoMoqDataAttribute()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: blue">base</span>(<span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>()))</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">}</pre></div>
<p>This is a custom attribute that combines AutoFixture's two optional extensions for auto-mocking and xUnit.net support. With the AutoMoqDataAttribute in place, we can now rewrite the above test like this:</p><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 Theory\cf0 , \cf3 AutoMoqData\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly([\cf3 Frozen\cf0 ]\cf3 Basket\cf0  basket,\par ??    [\cf3 Frozen\cf0 ]\cf3 Mock\cf0 &lt;\cf3 IPizzaMap\cf0 &gt; mapMock, \cf3 PizzaPresenter\cf0  pizza,\par ??    \cf3 BasketPresenter\cf0  sut)\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \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">Theory</span>, <span style="color: #2b91af">AutoMoqData</span>]</pre><pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly([<span style="color: #2b91af">Frozen</span>]<span style="color: #2b91af">Basket</span> basket,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">Frozen</span>]<span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt; mapMock, <span style="color: #2b91af">PizzaPresenter</span> pizza,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">BasketPresenter</span> sut)</pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; sut.Add(pizza);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>The AutoDataAttribute simply uses a Fixture object to create the objects declared in the unit tests parameter list. Notice that the test is no longer a [Fact], but rather a [Theory].</p>
<p>The only slightly tricky thing to notice is that when we declare a parameter object, it will automatically map to a call to the CreateAnonymous method for the parameter type. However, when we need to invoke the Freeze method, we can add the [Frozen] attribute in front of the parameter.</p>
<p>The best part about data theories is that they don't prevent us from writing normal unit tests in the same Test Class, and this carries over to the [AutoData] attribute. We can use it when it's possible, but for those more complex tests where we need to interact with a Fixture instance, we can still write a normal [Fact].</p>
</div>
	
<div id="comments">
<hr>
<h2 id="comments-header">
	Comments
</h2>
	<div class="comment">
	<div class="comment-author"><a href="http://colinbowern.com"><NAME></a></div>
	<div class="comment-content">I'm poking around at the AutoDataAttribute and I might be missing something but in a simple test I'm getting an InvalidOperationException:<br>
<br>
System.InvalidOperationException : No data found for XunitSample.TestClass.TestMethod2<br>
System.InvalidOperationException : No data found for XunitSample.TestClass.TestMethod1<br>
<br>
Did I miss something in terms of setup?  Sample code here - https://gist.github.com/1920943</div>
	<div class="comment-date">2012-02-27 02:49 UTC</div>
</div><div class="comment">
	<div class="comment-author"><a href="http://blog.ploeh.dk/"><NAME></a></div>
	<div class="comment-content">I just copied in the code from your gist and added the appropriate NuGet packages, followed by an Add-BindingRedirect, and everything works as intended. In other words, I can't reproduce the problem based on the information given here. I'd love to help, but I don't think this is the correct forum. Could you ask on Stack Overflow instead? If so, I'll take a look.</div>
	<div class="comment-date">2012-02-27 06:57 UTC</div>
</div><div class="comment">
	<div class="comment-author"><NAME></div>
	<div class="comment-content">I am very impressed with this way of doing things, but I have one question.  If you need to do setup on a mock to force certain return values, are you forced to go back to using a Fact and setting up the mock manually?</div>
	<div class="comment-date">2012-03-02 17:00 UTC</div>
</div><div class="comment">
	<div class="comment-author"><a href="http://blog.ploeh.dk"><NAME></a></div>
	<div class="comment-content">Absolutely, that's what the [Frozen] attribute is for. <a href="https://github.com/ploeh/Booking/blob/master/BookingDomainModel.UnitTest/CapacityGateTests.cs">Here's an example (second test).</a></div>
	<div class="comment-date">2012-03-03 19:42 UTC</div>
</div>
</div><div class="comment">
	<div class="comment-author"><NAME></div>
	<div class="comment-content"><p></p>Forgive me if I missed this point in your blog, but it seems that the parameter order is critical when injecting objects marked with the [Frozen] attribute into your test, if the SUT is being injected with that frozen object.</p>I was refactoring my tests to make use of AutoData like you've shown here, and was adding my [Frozen]Mock<IDependency> parameter willy-nilly to the end of the test parameters, *after* the SUT parameter. I was freezing it so that I could verify the same mocked dependency that was injected into the SUT as part of my test, or so I thought...<p>void MySutTest(MySut sut, [Frozen]Mock<IMySutDependency> frozen)</p><p>After a brief episode of confusion and self-loathing, I moved the frozen mocked dependency in front of the SUT in the test parameter list. Skies parted, angels sang, test passed again.</p><p>void MySutTest([Frozen]Mock<IMySutDependency> frozen, MySut sut)</p></div>
	<div class="comment-date">2013-08-22 23:00 UTC</div>
</div><commit_msg>HTML encoded angle brackets<commit_after>---
layout: post
tags: [AutoFixture, Unit Testing]
date: 2010-10-08 08:48:07 UTC
title: "AutoData Theories with AutoFixture"
---
{% include JB/setup %}

<div id="post">
	<p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0 comes with a new extension for <a href="http://xunit.codeplex.com/">xUnit.net</a> <a href="http://blog.benhall.me.uk/2008/01/introduction-to-xunitnet-extensions.html">data theories</a>. For those of us using xUnit.net, it can help make our unit tests more succinct and declarative.</p> <blockquote> <p>AutoFixture's support for xUnit.net is implemented in a separate assembly. AutoFixture itself has no dependency on xUnit.net, and if you use another unit testing framework, you can just ignore the existence of the Ploeh.AutoFixture.Xunit assembly.</p></blockquote> <p>Let's go back and revisit <a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx">the previous test</a> we wrote using AutoFixture and its auto-mocking extension:</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">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>());</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <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">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;();</pre><pre style="margin: 0px">&nbsp;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;();</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; sut.Add(pizza);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>Notice how all of the Fixture Setup phase is only used to create various objects that will be used in the test. First we create the fixture object, and then we use it to create four other objects. That turns out to be a pretty common idiom when using AutoFixture, so it's worthwhile to reduce the clutter if possible.</p>
<p>With xUnit.net's excellent extensibility features, we can. AutoFixture 2.0 now includes the AutoDataAttribute in a separate assembly. AutoDataAttribute derives from xUnit.net's DataAttribute (just like InlineDataAttribute), and while we can use it as is, it becomes really powerful if we combine it with auto-mocking 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  \cf1 class\cf0  \cf4 AutoMoqDataAttribute\cf0  : \cf4 AutoDataAttribute\par ??\cf0 \{\par ??    \cf1 public\cf0  AutoMoqDataAttribute()\par ??        : \cf1 base\cf0 (\cf1 new\cf0  \cf4 Fixture\cf0 ()\par ??            .Customize(\cf1 new\cf0  \cf4 AutoMoqCustomization\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">AutoMoqDataAttribute</span> : <span style="color: #2b91af">AutoDataAttribute</span></pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> AutoMoqDataAttribute()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: blue">base</span>(<span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>()</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>()))</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; {</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; }</pre><pre style="margin: 0px">}</pre></div>
<p>This is a custom attribute that combines AutoFixture's two optional extensions for auto-mocking and xUnit.net support. With the AutoMoqDataAttribute in place, we can now rewrite the above test like this:</p><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 Theory\cf0 , \cf3 AutoMoqData\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  AddWillPipeMapCorrectly([\cf3 Frozen\cf0 ]\cf3 Basket\cf0  basket,\par ??    [\cf3 Frozen\cf0 ]\cf3 Mock\cf0 &lt;\cf3 IPizzaMap\cf0 &gt; mapMock, \cf3 PizzaPresenter\cf0  pizza,\par ??    \cf3 BasketPresenter\cf0  sut)\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \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">Theory</span>, <span style="color: #2b91af">AutoMoqData</span>]</pre><pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly([<span style="color: #2b91af">Frozen</span>]<span style="color: #2b91af">Basket</span> basket,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">Frozen</span>]<span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt; mapMock, <span style="color: #2b91af">PizzaPresenter</span> pizza,</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">BasketPresenter</span> sut)</pre><pre style="margin: 0px">{</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; sut.Add(pizza);</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span></pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add));</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span></pre><pre style="margin: 0px">}</pre></div>
<p>The AutoDataAttribute simply uses a Fixture object to create the objects declared in the unit tests parameter list. Notice that the test is no longer a [Fact], but rather a [Theory].</p>
<p>The only slightly tricky thing to notice is that when we declare a parameter object, it will automatically map to a call to the CreateAnonymous method for the parameter type. However, when we need to invoke the Freeze method, we can add the [Frozen] attribute in front of the parameter.</p>
<p>The best part about data theories is that they don't prevent us from writing normal unit tests in the same Test Class, and this carries over to the [AutoData] attribute. We can use it when it's possible, but for those more complex tests where we need to interact with a Fixture instance, we can still write a normal [Fact].</p>
</div>
	
<div id="comments">
<hr>
<h2 id="comments-header">
	Comments
</h2>
	<div class="comment">
	<div class="comment-author"><a href="http://colinbowern.com"><NAME></a></div>
	<div class="comment-content">I'm poking around at the AutoDataAttribute and I might be missing something but in a simple test I'm getting an InvalidOperationException:<br>
<br>
System.InvalidOperationException : No data found for XunitSample.TestClass.TestMethod2<br>
System.InvalidOperationException : No data found for XunitSample.TestClass.TestMethod1<br>
<br>
Did I miss something in terms of setup?  Sample code here - https://gist.github.com/1920943</div>
	<div class="comment-date">2012-02-27 02:49 UTC</div>
</div><div class="comment">
	<div class="comment-author"><a href="http://blog.ploeh.dk/"><NAME></a></div>
	<div class="comment-content">I just copied in the code from your gist and added the appropriate NuGet packages, followed by an Add-BindingRedirect, and everything works as intended. In other words, I can't reproduce the problem based on the information given here. I'd love to help, but I don't think this is the correct forum. Could you ask on Stack Overflow instead? If so, I'll take a look.</div>
	<div class="comment-date">2012-02-27 06:57 UTC</div>
</div><div class="comment">
	<div class="comment-author"><NAME></div>
	<div class="comment-content">I am very impressed with this way of doing things, but I have one question.  If you need to do setup on a mock to force certain return values, are you forced to go back to using a Fact and setting up the mock manually?</div>
	<div class="comment-date">2012-03-02 17:00 UTC</div>
</div><div class="comment">
	<div class="comment-author"><a href="http://blog.ploeh.dk"><NAME></a></div>
	<div class="comment-content">Absolutely, that's what the [Frozen] attribute is for. <a href="https://github.com/ploeh/Booking/blob/master/BookingDomainModel.UnitTest/CapacityGateTests.cs">Here's an example (second test).</a></div>
	<div class="comment-date">2012-03-03 19:42 UTC</div>
</div>
</div><div class="comment">
	<div class="comment-author"><NAME></div>
	<div class="comment-content"><p></p>Forgive me if I missed this point in your blog, but it seems that the parameter order is critical when injecting objects marked with the [Frozen] attribute into your test, if the SUT is being injected with that frozen object.</p>I was refactoring my tests to make use of AutoData like you've shown here, and was adding my [Frozen]Mock&lt;IMySutDependency&gt; parameter willy-nilly to the end of the test parameters, *after* the SUT parameter. I was freezing it so that I could verify the same mocked dependency that was injected into the SUT as part of my test, or so I thought...<p>void MySutTest(MySut sut, [Frozen]Mock&lt;IMySutDependency&gt; frozen)</p><p>After a brief episode of confusion and self-loathing, I moved the frozen mocked dependency in front of the SUT in the test parameter list. Skies parted, angels sang, test passed again.</p><p>void MySutTest([Frozen]Mock&lt;IMySutDependency&gt; frozen, MySut sut)</p></div>
	<div class="comment-date">2013-08-22 23:00 UTC</div>
</div>
{'fraction_non_alphanumeric': 0.17844213476585308, 'fraction_numerical': 0.03417805286309315, 'mean_word_length': 5.587179487179487, 'pattern_counts': {'":': 0, '<': 568, '<?xml version=': 0, '>': 568, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3014653', 'n_tokens_mistral': 9024, 'n_tokens_neox': 8486, 'n_words': 2352}