AutoConfiguredMoqCustomization and unsettable properties

How to force AutoFixture using AutoConfiguredMoqCustomization to automatically simulate read-only interfaces and its properties?

To make everything clear, let's say that I have this interface:

public interface A { int Property {get;} } 

and such a class:

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

I should have had dependency for a layout 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 
+6
source share
1 answer

This is due to an error introduced in Moq 4.2.1502.911, where SetupAllProperties overrides previous settings made using get-only properties.

Here's a simpler reprogramming:

 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 what AutoFixture does behind the scenes to create an instance of Interface . This test fails with Moq versions equal to or greater than 4.2.1502.911, but passes with lower versions.

Just run this in the package manager console:

 install-package Moq -version 4.2.1409.1722 

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

+5
source

All Articles