Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, I'm going to answer this question under the assumption that <code>TypesWithoutPublicCtrs</code> is <a href="https://github.com/DavidSSL/AutoFixtureDeclarative/blob/0.0.1/Examples/TypesWithoutPublicCtrs.cs" rel="nofollow">defined as in the OP's GitHub repository</a>:</p> <pre><code>public class TypesWithoutPublicCtrs { private readonly IMyInterface _mi; public TypesWithoutPublicCtrs(IMyInterface mi) { _mi = mi; } } </code></pre> <p>The reason I'm explicitly calling this out is because the name is a red herring: it <em>does</em> have a public constructor; it just doesn't have a <em>default</em> constructor.</p> <p>Anyway, AutoFixture easily deals with the absence of default constructors. The problem here isn't the <code>TypesWithoutPublicCtrs</code> class itself, but rather the <code>IMyInterface</code> interface. Interfaces are problematic because they can't be initialized at all.</p> <p>Thus, you need to somehow map an interface to a concrete class. There are various ways to do that.</p> <p><strong>One-off solution</strong></p> <p>Once in a while, I use this one-off solution, although I find it ugly. However, it's easy and doesn't require a lot of sophisticated setup.</p> <pre><code>[Theory, AutoData] public void TestSomething( [Frozen(As = typeof(IMyInterface))]FakeMyInterface dummy, TypesWithoutPublicCtrs sut) { // use sut here, and ignore dummy } </code></pre> <p>This isn't particularly nice, because it relies on a side-effect of the <code>[Frozen]</code> attribute, but it works as a self-contained one-off solution.</p> <p><strong>Convention</strong></p> <p>However, I much rather like to make a convention out of it, so that the same convention applies for all tests in a test suite. A test using such a convention could look like this:</p> <pre><code>[Theory, MyTestConventions] public void TestSomething(TypesWithoutPublicCtrs sut) { // use sut here; it'll automatically have had FakeMyInterface injected } </code></pre> <p>The <code>[MyTestConventions]</code> attribute could look like this:</p> <pre><code>public class MyTestConventionsAttribute : AutoDataAttribute { public MyTestConventionsAttribute() : base(new Fixture().Customize(new MyTestConventions()) {} } </code></pre> <p>The <code>MyTestConventions</code> class must implement the interface <code>ICustomization</code>. There are several ways in which you can map <code>IMyInterface</code> to <code>FakeMyInterface</code>; here's one:</p> <pre><code>public class MyTestConventions : ICustomization { public void Customize(IFixture fixture) { fixture.Customizations.Add( new TypeRelay(typeof(IMyInterface), typeof(FakeMyInterface))); } } </code></pre> <p><strong>Auto-Mocking</strong></p> <p>However, you may get tired of having to create and maintain all those Fakes, so you can also turn AutoFixture into an <a href="http://blog.ploeh.dk/2013/03/11/auto-mocking-container" rel="nofollow">Auto-Mocking Container</a>. There are various options for doing that, <a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureasanauto-mockingcontainer/" rel="nofollow">leveraging Moq</a>, <a href="http://nuget.org/packages/AutoFixture.AutoNSubstitute/" rel="nofollow">NSubstitute</a>, <a href="http://nuget.org/packages/AutoFixture.AutoFakeItEasy" rel="nofollow">FakeItEasy</a> and <a href="http://nuget.org/packages/AutoFixture.AutoRhinoMocks/" rel="nofollow">Rhino Mocks</a>.</p>
    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. 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.
    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