New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add extension methods for Mock<T> setup on fixture customization #696
Conversation
Thank you for your interest in contributing to AutoFixture. Could you share a scenario that demonstrates usage of one or two of these methods? |
This is a typical test method with mock customization usage from our project: [TestMethod]
public async Task OnlineServiceLicenseActivationService_WhenActivationFailedWithATemporalProblem_ReturnsRetryResult()
{
// arrange
var fixture = PrepareFixture();
var command = fixture.Create<ActivateOnlineServiceLicense>();
var activationResult = CreateErrorResult(LicenseActivationFailureReason.ActivationServiceTemporalProblem);
fixture.CustomizeMock<ILicenseActivator>(mock => mock
.Setup(activator => activator.ActivateAsync(
It.IsAny<UserId>(),
It.IsAny<LicenseId>(),
It.IsAny<ActivationCode>()))
.Returns(activationResult.YieldTask()));
// act
var service = fixture.Create<OnlineServiceLicenseActivationService>();
var result = await service.ExecuteAsync(command);
// assert
Assert.IsTrue(result.Retry);
} I can add more if you want, but they are almost the same. |
How is fixture.CustomizeMock<ILicenseActivator>(mock => mock
.Setup(activator => activator.ActivateAsync(
It.IsAny<UserId>(),
It.IsAny<LicenseId>(),
It.IsAny<ActivationCode>()))
.Returns(activationResult.YieldTask())); better than fixture.Customize<Mock<ILicenseActivator>>(c => c.Do(mock => mock
.Setup(activator => activator.ActivateAsync(
It.IsAny<UserId>(),
It.IsAny<LicenseId>(),
It.IsAny<ActivationCode>()))
.Returns(activationResult.YieldTask()))); ? |
Thanks for your efforts on making this PR! I've reviewed your PR and would like to share my thoughts. In my opinion AutoFixture has very wide consumers range and it cannot provide helpers for all the cases out of the box. Rather, it's a set of building blocks and later users create their own usage patterns. The proposed helpers add very tiny value comparing to the native syntax, while they introduce yet another variance to achieve the same things. Given that I'd vote to not include these changes. However, I'd like to ask @moodmosaic, @ecampidoglio and @adamchester to share their thoughts regarding this one. @ar3cka Please let us know if you have some additional arguments we are missing. |
There are very few chances that this PR will be finally merged, so I'm closing it. Hope in future we'll have some sort of auxiliary project that could host such sort of things. @ar3cka Thank you again for your contribution, time and efforts! If you have concerns regarding such a decision, please let us know. |
Add useful extension methods (CustomizeMock and FreezeMock) for Mock setup