<commit_before>---
layout: post
tags: [AutoFixture]
date: 2009-12-05 00:41:45 UTC
title: "Building and assigning arrays with AutoFixture"
---
{% include JB/setup %}

<div id="post">
	<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">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassB</span>[] Items;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassC</span> C;</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; <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">&nbsp;&nbsp;&nbsp; .With(x =&gt; x.Items, </pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray())</pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp; .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">&nbsp;&nbsp;&nbsp; ob.With(x =&gt; x.Items, </pre><pre style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>
</div>
	
<div id="comments">
<hr>
<h2 id="comments-header"><commit_msg>Removed redundant (and ugly) pre tags from a post.<commit_after>---
layout: post
tags: [AutoFixture]
date: 2009-12-05 00:41:45 UTC
title: "Building and assigning arrays with AutoFixture"
---
{% include JB/setup %}

<div id="post">
	<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>
  <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>
{
&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassB</span>[] Items;
&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassC</span> C;
&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassD</span> D;
}</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>
<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;();
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>
<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;()
&nbsp;&nbsp;&nbsp; .With(x =&gt; x.Items, 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray())
&nbsp;&nbsp;&nbsp; .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>
<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;
&nbsp;&nbsp;&nbsp; ob.With(x =&gt; x.Items, 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray()));</pre></div>
<p>With this customization, every time we invoke</p>
<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>
</div>
	
<div id="comments">
<hr>
<h2 id="comments-header">
{'fraction_non_alphanumeric': 0.17652403016262377, 'fraction_numerical': 0.04433542291269192, 'mean_word_length': 6.088216355441082, 'pattern_counts': {'":': 0, '<': 224, '<?xml version=': 0, '>': 224, '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': '137727', 'n_tokens_mistral': 4069, 'n_tokens_neox': 3728, 'n_words': 953}