If you look at the stack trace, you will notice that the exception occurs deep inside Moq. AutoFixture is a self-confident library, and one of its opinions is that nulls are invalid return values . For this reason, AutoMoqCustomization configures all Mock instances as follows:
mock.DefaultValue = DefaultValue.Mock;
(by the way). Thus, you can fully reproduce the failed test without AutoFixture:
[Fact] public void ReproWithoutAutoFixture() { var factory = new Mock<IBuilderFactory>(); factory.DefaultValue = DefaultValue.Mock; var sut = new BuilderService(factory.Object); sut.Create();
It is strange that it still works with private classes. This, however, is not entirely true, but rather occurs in the OP tests, which are False Negatives .
Consider this Moq characterization test :
[Fact] public void MoqCharacterizationForUnsealedClass() { var factory = new Mock<IBuilderFactory>(); factory.DefaultValue = DefaultValue.Mock; Assert.Throws<ArgumentException>(() => factory.Object.Create()); }
Moq correctly throws an exception because he was asked to create an instance of CubeBuilder, and he does not know how to do it, because CubeBuilder does not have a default constructor, and no Setup tells him how to handle Create calls.
(In isolation, the irony is that AutoFixture may well create an instance of CubeBuilder, but Moq does not have an extensibility point that allows AutoFixture to enter and handle the behavior of an instance of the default Moq object.)
Now consider this feature test when the return type is sealed:
[Fact] public void MoqCharacterizationForSealedClass() { var factory = new Mock<IBuilderFactoryForSealedBuilder>(); factory.DefaultValue = DefaultValue.Mock; var actual = factory.Object.Create(); Assert.Null(actual); }
It turns out that in this case, despite the fact that it was implicitly told not to return null , Moq still does.
My theory is that what is actually happening is that in MoqCharacterizationForUnsealedClass above that factory.DefaultValue = DefaultValue.Mock; really means that Moq creates a CubeBuilder layout - in other words, it dynamically emits a class that derives from CubeBuilder. However, when asked to create a SealedCubeBuilder layout, it cannot, because it cannot create a class derived from a private class.
Instead of throwing an exception, it returns null . This is inconsistent behavior, and I reported it as a bug in Moq .