5

I've tried using the AutoMoqCustomization in order to auto-mock an object graph with nested dependencies and results are not as I expected. Below is the failing test code (assertions using FluentAssertions). When running the test, the IDependant injected to Dependant2 has a different IObject mock than the one frozen earlier. Is this a bug in mocks freezing, or am I not understanding correctly how to use it?

Note: I read this regarding using AutoConfiguredMoqCustomization and it doesn't work when IDependant has a property IObject Obj { get; } but does work when it has a method IObject GetObj();, is this the expected behavior of AutoConfiguredMoqCustomization?

The failing test code:

[TestClass]
public class MyTestClass
{
     [TestMethod]
     public void Test()
     {
         var fixture = new Fixture().Customize(new AutoMoqCustomization());

         var objMock = fixture.Freeze<Mock<IObject>>();
         var sut = fixture.Create<Dependant2>();

         sut.Obj.Should().BeSameAs(objMock);
     }
}

public interface IObject { }

public interface IDependant { IObject Obj { get; } }

public class MyObject : IObject { }

public class Dependant2
{
    public Dependant2(IDependant dependant)
    {
       Obj = dependant.Obj; 
    }

    public IObject Obj { get; }
}
Community
  • 1
  • 1
Eldar S
  • 579
  • 3
  • 17
  • 1
    This question was also asked and resolved in [the AutoFixture GitHub repository](https://github.com/AutoFixture/AutoFixture/issues/608). – Mark Seemann Apr 16 '16 at 10:55
  • 2
    it was *closed*, but I would not call it resolved. It's due to a bug in Moq that hasn't been fixed, and the only workaround I've been able to find is to use an older version of Moq – bj0 Jun 21 '16 at 23:09

0 Answers0