<commit_before>---
layout: post
tags: [AutoFixture, Unit Testing]
date: 2011-02-07 19:49:26 UTC
title: "Creating specific populated lists with AutoFixture"
---
{% include JB/setup %}

<div id="post">
	<p>How do you get <a href="http://autofixture.codeplex.com/">AutoFixture</a> to create populated lists or sequences of items? Recently I seem to have been getting this question a lot, and luckily it's quite easy to answer.</p> <p>Let's first look at the standard AutoFixture behavior and API.</p> <p>You can ask AutoFixture to create an anonymous List like this:</p> <div style="font-family: ; background: white"><pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> list = fixture.CreateAnonymous&lt;<span><font color="#2b91af">List</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre></div>
<p>Seen from AutoFixture's point of view, List&lt;int&gt; is just a class like any other. It has a default constructor, so AutoFixture just uses that and returns an instance. You get back an instance, no exceptions are thrown, but the list is empty. What if you'd rather want a populated list?</p>
<p>There are many ways to go about this. A simple, low-level solution is to populate the list after creation:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.AddManyTo(list);</font></pre></div>
<p>However, you may instead prefer getting a populated list right away. This is also possible, but before we look at how to get there, I'd like to point out a feature that surprisingly few users notice. You can <a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx">create many anonymous specimens at once</a>:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> integers = fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;();</font></pre></div>
<p>Armed with this knowledge, as well as the knowledge of <a href="http://blog.ploeh.dk/2010/04/06/MappingTypesWithAutoFixture.aspx">how to map types</a>, we can now create this customization to map IEnumerable&lt;int&gt; to CreateMany&lt;int&gt;:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;());</font></pre></div>
<p>The Register method is really a generic method, but since we have type inference, we don't have to write it out. However, since CreateMany&lt;int&gt;() returns IEnumerable&lt;int&gt;, this is the type we register. Thus, every time we subsequently resolve IEnumerable&lt;int&gt;, we will get back a populated sequence.</p>
<p>Getting back to the original List&lt;int&gt; example, we can now customize it to a populated list like this:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt;</font></pre><pre style="margin: 0px"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList());</font></pre></div>
<p>Because the ToList() extension method returns List&lt;T&gt;, this call registers List&lt;int&gt; so that we will get back a populated list of integers every time the fixture resolves List&lt;int&gt;.</p>
<p>What about other collection types that don't have a nice LINQ extension method? Personally, I never use Collection&lt;T&gt;, but if you wanted, you could customize it like this:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt;</font></pre><pre style="margin: 0px"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Collection</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;(</font></pre><pre style="margin: 0px"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList()));</font></pre></div>
<p>Since Collection&lt;T&gt; has a constructor overload that take IList&lt;T&gt; we can customize the type to use this specific overload and populate it with ‘many' items.</p>
<p>Finally, we can combine all this to map from collection interfaces to populated lists. As an example, we can map from IList&lt;int&gt; to a populated List&lt;int&gt; like this:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register&lt;<span><font color="#2b91af">IList</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;(() =&gt; </font></pre><pre style="margin: 0px"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList());</font></pre></div>
<p>When we use the Register method to map types we can no longer rely on type inference. Instead, we must explicitly register IList&lt;int&gt; against a delegate that creates a populated List&lt;int&gt;. Because List&lt;int&gt; implements IList&lt;int&gt; this compiles. Whenever this fixture instance resolves IList&lt;int&gt; it will create a populated List&lt;int&gt;.</p>
<p>All of this describes what you can do with the strongly typed API available in AutoFixture 2.0. It's easy and very flexible, but the only important drawback is that it's not general. All of the customizations in this post specifically address lists and sequences of integers, but not lists of any other type. What if you would like to expand this sort of behavior to any List&lt;T&gt;, IEnumerable&lt;T&gt; etc?</p>
<p>Stay tuned, because in <a href="http://blog.ploeh.dk/2011/02/08/CreatingGeneralPopulatedListsWithAutoFixture.aspx">the next post I will describe how to do that</a>.</p>
</div>
	<commit_msg>Removed redundant (and ugly) pre tags from a post.<commit_after>---
layout: post
tags: [AutoFixture, Unit Testing]
date: 2011-02-07 19:49:26 UTC
title: "Creating specific populated lists with AutoFixture"
---
{% include JB/setup %}

<div id="post">
	<p>How do you get <a href="http://autofixture.codeplex.com/">AutoFixture</a> to create populated lists or sequences of items? Recently I seem to have been getting this question a lot, and luckily it's quite easy to answer.</p> <p>Let's first look at the standard AutoFixture behavior and API.</p> <p>You can ask AutoFixture to create an anonymous List like this:</p> <div style="font-family: ; background: white"><pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> list = fixture.CreateAnonymous&lt;<span><font color="#2b91af">List</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre></div>
<p>Seen from AutoFixture's point of view, List&lt;int&gt; is just a class like any other. It has a default constructor, so AutoFixture just uses that and returns an instance. You get back an instance, no exceptions are thrown, but the list is empty. What if you'd rather want a populated list?</p>
<p>There are many ways to go about this. A simple, low-level solution is to populate the list after creation:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.AddManyTo(list);</font></pre></div>
<p>However, you may instead prefer getting a populated list right away. This is also possible, but before we look at how to get there, I'd like to point out a feature that surprisingly few users notice. You can <a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx">create many anonymous specimens at once</a>:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> integers = fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;();</font></pre></div>
<p>Armed with this knowledge, as well as the knowledge of <a href="http://blog.ploeh.dk/2010/04/06/MappingTypesWithAutoFixture.aspx">how to map types</a>, we can now create this customization to map IEnumerable&lt;int&gt; to CreateMany&lt;int&gt;:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;());</font></pre></div>
<p>The Register method is really a generic method, but since we have type inference, we don't have to write it out. However, since CreateMany&lt;int&gt;() returns IEnumerable&lt;int&gt;, this is the type we register. Thus, every time we subsequently resolve IEnumerable&lt;int&gt;, we will get back a populated sequence.</p>
<p>Getting back to the original List&lt;int&gt; example, we can now customize it to a populated list like this:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt;</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList());</font></pre></div>
<p>Because the ToList() extension method returns List&lt;T&gt;, this call registers List&lt;int&gt; so that we will get back a populated list of integers every time the fixture resolves List&lt;int&gt;.</p>
<p>What about other collection types that don't have a nice LINQ extension method? Personally, I never use Collection&lt;T&gt;, but if you wanted, you could customize it like this:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt;</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Collection</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;(</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList()));</font></pre></div>
<p>Since Collection&lt;T&gt; has a constructor overload that take IList&lt;T&gt; we can customize the type to use this specific overload and populate it with ‘many' items.</p>
<p>Finally, we can combine all this to map from collection interfaces to populated lists. As an example, we can map from IList&lt;int&gt; to a populated List&lt;int&gt; like this:</p>
<div style="font-family: ; background: white"><pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register&lt;<span><font color="#2b91af">IList</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;(() =&gt; </font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList());</font></pre></div>
<p>When we use the Register method to map types we can no longer rely on type inference. Instead, we must explicitly register IList&lt;int&gt; against a delegate that creates a populated List&lt;int&gt;. Because List&lt;int&gt; implements IList&lt;int&gt; this compiles. Whenever this fixture instance resolves IList&lt;int&gt; it will create a populated List&lt;int&gt;.</p>
<p>All of this describes what you can do with the strongly typed API available in AutoFixture 2.0. It's easy and very flexible, but the only important drawback is that it's not general. All of the customizations in this post specifically address lists and sequences of integers, but not lists of any other type. What if you would like to expand this sort of behavior to any List&lt;T&gt;, IEnumerable&lt;T&gt; etc?</p>
<p>Stay tuned, because in <a href="http://blog.ploeh.dk/2011/02/08/CreatingGeneralPopulatedListsWithAutoFixture.aspx">the next post I will describe how to do that</a>.</p>
</div>
	
{'fraction_non_alphanumeric': 0.1642780217049126, 'fraction_numerical': 0.021207853533261534, 'mean_word_length': 6.343065693430657, 'pattern_counts': {'":': 0, '<': 315, '<?xml version=': 0, '>': 315, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3956948', 'n_tokens_mistral': 3936, 'n_tokens_neox': 3594, 'n_words': 1258}