Search code examples
c#moqautofixtureautomoq

AutoFixture Freeze returns empty object


I'm using AutoFixture with AutoMoqCustomization to provide any mocks of class dependencies needed.

I expected that when I test a class with a dependency, any calls to methods on that dependency would return an AutoFixtured instance of that object.

AutoFixture IS creating mocks for dependencies, and those mocks ARE returning objects, but the objects are empty. Anything nullable will be null, anything not nullable will be default value.

I understand that I could explicitly write

AutoFixture.Freeze<IDependent>()
    .Setup(x=>x.GetThing())
    .Returns(AutoFixture.Create<Thing>())

But I thought it would work without explicit setup. Is this expected behavior?


Solution

  • You have to tell AutoMoqCustomization to configure members as well. I guess default behavior changed in one of the releases:

    fixture.Customize(new AutoMoqCustomization {ConfigureMembers = true});
    

    Actually, that's what documentation of AutoMoqCustomization class suggests:

    /// <summary>
    /// Enables auto-mocking with Moq.
    /// </summary>
    /// <remarks>
    /// NOTICE! You can assign the customization properties to tweak the features you would like to enable. See example.
    /// <br />
    /// <code>new AutoMoqCustomization { ConfigureMembers = true }</code>
    /// </remarks>
    public class AutoMoqCustomization : ICustomization