Last active
December 16, 2021 21:22
-
-
Save aivascu/eb658ff05784541cc3f130839f27e227 to your computer and use it in GitHub Desktop.
Custom Mock postprocessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConfigurableMockPostprocessor : ISpecimenBuilder | |
{ | |
public ConfigurableMockPostprocessor(ISpecimenBuilder builder) | |
{ | |
this.Builder = builder ?? throw new ArgumentNullException(nameof(builder)); | |
} | |
public ISpecimenBuilder Builder { get; set; } | |
public object Create(object request, ISpecimenContext context) | |
{ | |
if (request is not Type t || !IsMock(t)) return new NoSpecimen(); | |
var specimen = this.Builder.Create(request, context); | |
if (specimen is NoSpecimen or OmitSpecimen or null) return specimen; | |
if (specimen is not Mock m) return new NoSpecimen(); | |
var mockType = GetMockedType(t); | |
if (GetMockedType(m.GetType()) != mockType) return new NoSpecimen(); | |
this.GetType() | |
?.GetMethod(nameof(this.ConfigureMock)) | |
?.MakeGenericMethod(mockType) | |
.Invoke(this, new object[] { m }); | |
return m; | |
} | |
public virtual void ConfigureMock<T>(Mock<T> mock) | |
where T : class | |
{ | |
mock.CallBase = ShouldUseCallBase(typeof(T)); | |
mock.DefaultValue = DefaultValue.Empty; | |
} | |
protected static bool ShouldUseCallBase(Type type) => !IsDelegate(type); | |
private static bool IsDelegate(Type type) | |
=> typeof(MulticastDelegate) | |
.IsAssignableFrom(type.GetTypeInfo().BaseType); | |
private static Type GetMockedType(Type type) | |
=> type.GetTypeInfo().GetGenericArguments().Single(); | |
private static bool IsMock(Type type) | |
=> type != null | |
&& type.GetTypeInfo().IsGenericType | |
&& typeof(Mock<>).IsAssignableFrom(type.GetGenericTypeDefinition()) | |
&& !GetMockedType(type).IsGenericParameter; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmptyMockCustomization : ICustomization | |
{ | |
public void Customize(IFixture fixture) | |
{ | |
fixture.Customizations.Insert(0, | |
new ConfigurableMockPostprocessor( | |
new MethodInvoker( | |
new MockConstructorQuery()))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void MyTestCase1() | |
{ | |
var fixture = new Fixture().Customize( | |
new CompositeCustomization( | |
new AutoMoqCustomization | |
{ | |
ConfigureMembers = false, | |
GenerateDelegates = true | |
}, | |
new EmptyMockCustomization())); | |
var dep = fixture.Freeze<Mock<IDep>>(); | |
var myClass = fixture.Create<MyClass>(); | |
Assert.Null(dep.Object.Get(5)); //false | |
Assert.Null(myClass.Method(5)); //false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment