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
Julian
  • 33,915
  • 22
  • 119
  • 174
SOReader
  • 5,697
  • 5
  • 31
  • 53
  • 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`. – dcastro Aug 18 '15 at 10:43

1 Answers1

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

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • 1
    +1 It might be worth mentioning that we're also tracking the issue in AutoFixture itself: https://github.com/AutoFixture/AutoFixture/issues/434 – Mark Seemann Aug 18 '15 at 15:05
  • 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. – SOReader Aug 19 '15 at 07:22