AutoConfiguredMoqCustomization and unsettable properties
Asked Answered
G

1

7

How do I force AutoFixture, that has been configured with AutoConfiguredMoqCustomization, to automatically mock interfaces and its read-only properties?

To make things clear, let's assume I have such an interface:

public interface A {
     int Property {get;}
}

and such class:

public class SomeClass {
     public SomeClass(A dependency) {}
}

What I want is to have dependency resolved to a mock that will return something in dependency.Property:

var fixture = new Fixture().Customize(new AutoConfiguredMoqCustomization());
var sut = fixture.Create<SomeClass>(); // <- dependency passed to SomeClass' constructor will have .Property returning null
Goatish answered 18/8, 2015 at 8:48 Comment(1)
I can only reproduce this with the latest version of Moq - try installing version 4.1.1308.2120 instead: install-package Moq -version 4.1.1308.2120.Peccadillo
P
5

This is due to a bug introduced in Moq 4.2.1502.911, where SetupAllProperties overrides previous setups done on get-only properties.

Here's a simpler repro:

public interface Interface
{
    string Property { get; }
}

var a = new Mock<Interface>();

a.Setup(x => x.Property).Returns("test");
a.SetupAllProperties();

Assert.NotNull(a.Object.Property);

This is sort of what AutoFixture does behind the scenes to create an instance of Interface. This test fails with versions of Moq equal to or greater than 4.2.1502.911, but passes with lower versions.

Simply run this on the Package Manager Console:

install-package Moq -version 4.2.1409.1722

This bug is being tracked here: https://github.com/Moq/moq4/issues/196

Peccadillo answered 18/8, 2015 at 12:54 Comment(2)
+1 It might be worth mentioning that we're also tracking the issue in AutoFixture itself: github.com/AutoFixture/AutoFixture/issues/434Unlikely
Thanks for your help. I didn't have time to try your workaround yet. What I did was changing the properties to be settable, and it worked fine.Goatish

© 2022 - 2024 — McMap. All rights reserved.