Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Go read </p> <ul> <li><a href="http://nikosbaxevanis.com/categories/autofixture/" rel="nofollow">Nikos Baxevanis's blog</a></li> <li><a href="http://megakemp.com/tag/autofixture/" rel="nofollow">Enrico Camidoglio's blog</a></li> <li><a href="http://blog.ploeh.dk/tags.html#AutoFixture-ref" rel="nofollow">Mark Seemann's AutoFixture posts</a> (<a href="http://blog.ploeh.dk/2011/03/18/EncapsulatingAutoFixtureCustomizations/" rel="nofollow">Encapsulating AutoFixture Customizations</a> is the best) </li> </ul> <p>for great examples of customizations and how to package, mix and mingle them.</p> <p>Main principle is that you make your Customizations as granular as possible.</p> <p>Then, you need to feed them in to the processing pipeline via either:</p> <ul> <li><code>AutoData</code>-derived attributes for global stuff (i.e. like <code>MyTestConventions</code> in Mark's answer )</li> <li>A <code>CustomizeWith</code> helper[1] or similar</li> <li>Trickery such as doing a <code>[Freeze( As ... )]</code></li> </ul> <h1>My implementation</h1> <p>Automating this, I'd write:</p> <pre><code>[Theory, AutoData] public static void OutOfBandCustomization( [CustomizeWith( typeof( MyFakerCustomization ) )] MyClass sut ) { } </code></pre> <p>Using this customization:</p> <pre><code>public class MyFakerCustomization : ICustomization { void ICustomization.Customize( IFixture fixture ) { var knownText = "This text is not anonymous"; fixture.Register&lt;int, IMyInterface&gt;( i =&gt; new FakeMyInterface( i, knownText ) ); } } </code></pre> <p>Obviously registering a <code>string</code> and/or using <code>AutoMoqCustomization</code> might also be useful.</p> <h1>My general helpers</h1> <p>[1] <code>CustomizeWith</code> is this helper Attribute (hat tip to <a href="https://github.com/adamjasinski" rel="nofollow">Adam Jasinski</a>):</p> <pre><code>[AttributeUsage( AttributeTargets.Parameter, AllowMultiple = true )] sealed class CustomizeWithAttribute : CustomizeAttribute { readonly Type _type; public CustomizeWithAttribute( Type customizationType ) { if ( customizationType == null ) throw new ArgumentNullException( "customizationType" ); if ( !typeof( ICustomization ).IsAssignableFrom( customizationType ) ) throw new ArgumentException( "Type needs to implement ICustomization" ); _type = customizationType; } public override ICustomization GetCustomization( ParameterInfo parameter ) { return (ICustomization)Activator.CreateInstance( _type ); } } </code></pre> <h1>Aside</h1> <p>One tip: you can express </p> <pre><code>fixture.Register&lt;int, IMyInterface&gt;( i =&gt; new FakeMyInterface( i, knownText ) ); </code></pre> <p>as</p> <pre><code> fixture.Customize&lt;IMyInterface&gt;(c =&gt;c.FromFactory((int i)=&gt; new FakeMyInterface(i,knownText))); </code></pre> <p>too. While this doesn't make your case easier, it is a more general way of customizing what's going on.</p> <p>Internally, <code>Register</code> is [currently]:</p> <pre><code>fixture.Customize&lt;T&gt;(c =&gt; c.FromFactory(creator).OmitAutoProperties()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload