Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:49:57+00:00 2026-06-14T19:49:57+00:00

The short takeaway now that the solution has been found: AutoFixture returns frozen the

  • 0

The short takeaway now that the solution has been found:

AutoFixture returns frozen the mock just fine; my sut that was also generated by AutoFixture just had a public property with a local default that was important for the test and that AutoFixture set to a new value. There is much to learn beyond that from Mark’s answer.

Original question:

I started trying out AutoFixture yesterday for my xUnit.net tests that have Moq all over them. I was hoping to replace some of the Moq stuff or make it easier to read, and I’m especially interested in using AutoFixture in the SUT Factory capacity.

I armed myself with a few of Mark Seemann’s blog posts on AutoMocking and tried to work from there, but I didn’t get very far.

This is what my test looked like without AutoFixture:

[Fact]
public void GetXml_ReturnsCorrectXElement()
{
    // Arrange
    string xmlString = @"
        <mappings>
            <mapping source='gcnm_loan_amount_min' target='gcnm_loan_amount_min_usd' />
            <mapping source='gcnm_loan_amount_max' target='gcnm_loan_amount_max_usd' />
        </mappings>";

    string settingKey = "gcCreditApplicationUsdFieldMappings";

    Mock<ISettings> settingsMock = new Mock<ISettings>();
    settingsMock.Setup(s => s.Get(settingKey)).Returns(xmlString);
    ISettings settings = settingsMock.Object;

    ITracingService tracing = new Mock<ITracingService>().Object;

    XElement expectedXml = XElement.Parse(xmlString);

    IMappingXml sut = new SettingMappingXml(settings, tracing);

    // Act
    XElement actualXml = sut.GetXml();

    // Assert
    Assert.True(XNode.DeepEquals(expectedXml, actualXml));
}

The story here is simple enough – make sure that SettingMappingXml queries the ISettings dependency with the correct key (which is hard coded/property injected) and returns the result as an XElement. The ITracingService is relevant only if there’s an error.

What I was trying to do is get rid of the need to explicitly create the ITracingService object and then manually inject the dependencies (not because this test is too complex, but because it is simple enough to try things out and understand them).

Enter AutoFixture – first attempt:

[Fact]
public void GetXml_ReturnsCorrectXElement()
{
    // Arrange
    IFixture fixture = new Fixture();
    fixture.Customize(new AutoMoqCustomization());

    string xmlString = @"
        <mappings>
            <mapping source='gcnm_loan_amount_min' target='gcnm_loan_amount_min_usd' />
            <mapping source='gcnm_loan_amount_max' target='gcnm_loan_amount_max_usd' />
        </mappings>";

    string settingKey = "gcCreditApplicationUsdFieldMappings";

    Mock<ISettings> settingsMock = new Mock<ISettings>();
    settingsMock.Setup(s => s.Get(settingKey)).Returns(xmlString);
    ISettings settings = settingsMock.Object;
    fixture.Inject(settings);

    XElement expectedXml = XElement.Parse(xmlString);

    IMappingXml sut = fixture.CreateAnonymous<SettingMappingXml>();

    // Act
    XElement actualXml = sut.GetXml();

    // Assert
    Assert.True(XNode.DeepEquals(expectedXml, actualXml));
}

I would expect CreateAnonymous<SettingMappingXml>(), upon detection of the ISettings constructor parameter, to notice that a concrete instance has been registered for that interface and inject that – however, it doesn’t do that but instead creates a new anonymous implementation.

This is especially confusing as fixture.CreateAnonymous<ISettings>() does indeed return my instance –

IMappingXml sut = new SettingMappingXml(fixture.CreateAnonymous<ISettings>(), fixture.CreateAnonymous<ITracingService>());

makes the test perfectly green, and this line is exactly what I had expected AutoFixture to do internally when instantiating SettingMappingXml.

Then there’s the concept of freezing a component, so I went ahead just freezing the mock in the fixture instead of getting the mocked object:

fixture.Freeze<Mock<ISettings>>(f => f.Do(m => m.Setup(s => s.Get(settingKey)).Returns(xmlString)));

Sure enough this works perfectly fine – as long as I call the SettingMappingXml constructor explicitly and don’t rely on CreateAnonymous().

Simply put, I don’t understand why it works the way it apparently does, as it goes against any logic I can conjure up.
Normally I would suspect a bug in the library, but this is something so basic that I’m sure others would have run into this and it would long have been found and fixed. What’s more, knowing Mark’s assiduous approach to testing and DI, this can’t be unintentional.

That in turn means I must be missing something rather elementary. How can I have my SUT created by AutoFixture with a preconfigured mocked object as a dependency? The only thing I’m sure about right now is that I need the AutoMoqCustomization so I don’t have to configure anything for the ITracingService.

AutoFixture/AutoMoq packages are 2.14.1, Moq is 3.1.416.3, all from NuGet. .NET version is 4.5 (installed with VS2012), behavior is the same in VS2012 and 2010.

While writing this post, I discovered that some people were having problems with Moq 4.0 and assembly binding redirects, so I meticulously purged my solution of any instances of Moq 4 and had Moq 3.1 installed by installing AutoFixture.AutoMoq into “clean” projects. However, the behavior of my test remains unchanged.

Thank you for any pointers and explanations.

Update: Here’s the constructor code Mark asked for:

public SettingMappingXml(ISettings settingSource, ITracingService tracing)
{
    this._settingSource = settingSource;
    this._tracing = tracing;

    this.SettingKey = "gcCreditApplicationUsdFieldMappings";
}

And for completeness, the GetXml() method looks like this:

public XElement GetXml()
{
    int errorCode = 10600;

    try
    {
        string mappingSetting = this._settingSource.Get(this.SettingKey);
        errorCode++;

        XElement mappingXml = XElement.Parse(mappingSetting);
        errorCode++;

        return mappingXml;
    }
    catch (Exception e)
    {
        this._tracing.Trace(errorCode, e.Message);
        throw;
    }
}

SettingKey is just an automatic property.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:49:59+00:00Added an answer on June 14, 2026 at 7:49 pm

    Assuming that the SettingKey property is defined as follows, I can now reproduce the issue:

    public string SettingKey { get; set; }
    

    What happens is that the Test Doubles injected into the SettingMappingXml instance are perfectly fine, but because the SettingKey is writable, AutoFixture’s Auto-properties feature kicks in and modifies the value.

    Consider this code:

    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var sut = fixture.CreateAnonymous<SettingMappingXml>();
    Console.WriteLine(sut.SettingKey);
    

    This prints something like this:

    SettingKey83b75965-2886-4308-bcc4-eb0f8e63de09

    Even though all the Test Doubles are properly injected, the expectation in the Setup method isn’t met.

    There are many ways to address this issue.

    Protect invariants

    The proper way to resolve this issue is to use the unit test and AutoFixture as a feedback mechanism. This is one of the key points in GOOS: problems with unit tests are often a symptom about a design flaw rather than the fault of the unit test (or AutoFixture) itself.

    In this case it indicates to me that the design isn’t fool-proof enough. Is it really appropriate that a client can manipulate the SettingKey at will?

    As a bare minimum, I would recommend an alternative implementation like this:

    public string SettingKey { get; private set; }
    

    With that change, my repro passes.

    Omit SettingKey

    If you can’t (or won’t) change your design, you can instruct AutoFixture to skip setting the SettingKey property:

    IMappingXml sut = fixture
        .Build<SettingMappingXml>()
        .Without(s => s.SettingKey)
        .CreateAnonymous();
    

    Personally, I find it counterproductive to have to write a Build expression every time I need an instance of a particular class. You can decouple how the SettingMappingXml instance are created from the actual instantiation:

    fixture.Customize<SettingMappingXml>(
        c => c.Without(s => s.SettingKey));
    IMappingXml sut = fixture.CreateAnonymous<SettingMappingXml>();
    

    To take this further, you can encapsulate that Customize method call in a Customization.

    public class SettingMappingXmlCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            fixture.Customize<SettingMappingXml>(
                c => c.Without(s => s.SettingKey));
        }
    }
    

    This requires you to create your Fixture instance with that Customization:

    IFixture fixture = new Fixture()
        .Customize(new SettingMappingXmlCustomization())
        .Customize(new AutoMoqCustomization());
    

    Once you get more than two or three Customizations to chain, you may get tired of writing that method chain all the time. It’s time to encapsulate those Customizations into a set of conventions for your particular library:

    public class TestConventions : CompositeCustomization
    {
        public TestConventions()
            : base(
                new SettingMappingXmlCustomization(),
                new AutoMoqCustomization())
        {
        }
    }
    

    This enables you to always create the Fixture instance like this:

    IFixture fixture = new Fixture().Customize(new TestConventions());
    

    The TestConventions gives you a central place where you can go and occasionally modify your conventions for the test suite when you need to do so. It reduces the maintainability tax of your unit tests and helps keep the design of your production code more consistent.

    Finally, since it looks as though you are using xUnit.net, you could utilize AutoFixture’s xUnit.net integration, but before you do that you’d need to use a less imperative style of manipulating the Fixture. It turns out that the code which creates, configures and injects the ISettings Test Double is so idiomatic that it has a shortcut called Freeze:

    fixture.Freeze<Mock<ISettings>>()
        .Setup(s => s.Get(settingKey)).Returns(xmlString);
    

    With that in place, the next step is to define a custom AutoDataAttribute:

    public class AutoConventionDataAttribute : AutoDataAttribute
    {
        public AutoConventionDataAttribute()
            : base(new Fixture().Customize(new TestConventions()))
        {
        }
    }
    

    You can now reduce the test to the bare essentials, getting rid of all the noise, enabling the test to succinctly express only what matters:

    [Theory, AutoConventionData]
    public void ReducedTheory(
        [Frozen]Mock<ISettings> settingsStub,
        SettingMappingXml sut)
    {
        string xmlString = @"
            <mappings>
                <mapping source='gcnm_loan_amount_min' target='gcnm_loan_amount_min_usd' />
                <mapping source='gcnm_loan_amount_max' target='gcnm_loan_amount_max_usd' />
            </mappings>";
        string settingKey = "gcCreditApplicationUsdFieldMappings";
        settingsStub.Setup(s => s.Get(settingKey)).Returns(xmlString);
    
        XElement actualXml = sut.GetXml();
    
        XElement expectedXml = XElement.Parse(xmlString);
        Assert.True(XNode.DeepEquals(expectedXml, actualXml));
    }
    

    Other options

    To make the original test pass, you could also just switch off Auto-properties entirely:

    fixture.OmitAutoProperties = true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short question: Is it possible to map a buffer that has been malloc'd to
Short one: Am I correct in assuming, that the Samsung Galaxy S wifi 4.0
Short and simple, our project has a Launcher component and an Application component and
Short Intro Currently I have a UITableView which is filled with custom cells that
Short of rolling your own. There has to be something out there. FlexLM/FlexNet is
Short Question: Given Clojure's concurrency model, how do I ensure that all LWJGL OpenGL
Short Version I want an ADPlus script that will do a full memory dump
Short version: How can I get a regex that matches a@a.aaaa but not a@a.aaaaa
Short During debug process I see that, all goes right. For debugging purposes, before
Short Generating table with barcodes of items. Each item has exact quantity in database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.