Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

ar3cka
Copy link

@ar3cka ar3cka commented Aug 19, 2016

Add useful extension methods (CustomizeMock and FreezeMock) for Mock setup

@ploeh
Copy link
Member

ploeh commented Aug 22, 2016

Thank you for your interest in contributing to AutoFixture. Could you share a scenario that demonstrates usage of one or two of these methods?

@ar3cka
Copy link
Author

ar3cka commented Aug 26, 2016

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.

@ploeh
Copy link
Member

ploeh commented Aug 27, 2016

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())));

?

@ploeh
Copy link
Member

ploeh commented Sep 7, 2016

@zvirja
Copy link
Member

zvirja commented Jun 27, 2017

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.

@moodmosaic
Copy link
Member

I agree with what @ploeh wrote and what @zvirja wrote.

@zvirja
Copy link
Member

zvirja commented Aug 14, 2017

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.

@zvirja zvirja closed this Aug 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants