Created
August 21, 2024 00:20
-
-
Save aivascu/b946a2fb6021d9648ae9d341ceb1c28b to your computer and use it in GitHub Desktop.
Mock concrete class
This file contains hidden or 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 AutoMockDataAttribute : AutoDataAttribute | |
| { | |
| public AutoMockDataAttribute() | |
| : base(() => new Fixture().Customize(new AutoMoqCustomization())) | |
| { | |
| } | |
| } | |
| public class InlineAutoMockDataAttribute : InlineAutoDataAttribute | |
| { | |
| public InlineAutoMockDataAttribute(params object[] values) | |
| : base(new AutoMockDataAttribute(), values) | |
| { | |
| } | |
| } |
This file contains hidden or 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
| [AttributeUsage(AttributeTargets.Parameter)] | |
| public class MockAttribute : Attribute, IParameterCustomizationSource | |
| { | |
| public ICustomization GetCustomization(ParameterInfo parameter) | |
| { | |
| return new MockedParameterCustomization(parameter); | |
| } | |
| private class MockedParameterCustomization : ICustomization | |
| { | |
| private readonly ParameterInfo parameter; | |
| public MockedParameterCustomization(ParameterInfo parameter) | |
| { | |
| this.parameter = parameter; | |
| } | |
| public void Customize(IFixture fixture) | |
| { | |
| var parameterType = parameter.ParameterType; | |
| if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition().Equals(typeof(Mock<>))) | |
| { | |
| var genericArgument = parameterType.GetGenericArguments().Single(); | |
| fixture.Customizations.Add(new MockRelay(new ExactTypeSpecification(genericArgument))); | |
| return; | |
| } | |
| fixture.Customizations.Add(new MockRelay(new ExactTypeSpecification(parameterType))); | |
| } | |
| } | |
| } |
This file contains hidden or 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 UnitTests | |
| { | |
| [Theory] | |
| [InlineAutoMockData(5)] | |
| public void Foo(int a, [Frozen][Mock]Mock<TimeProvider> providerMock, Wrapper<TimeProvider> wrapper) | |
| { | |
| Assert.Same(providerMock.Object, wrapper.Value); | |
| Assert.Equal(5, a); | |
| } | |
| } |
This file contains hidden or 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 Wrapper<T> | |
| { | |
| public Wrapper(T value) | |
| { | |
| Value = value; | |
| } | |
| public T Value { get; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment