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

Invoking public method on mocked instance invalidates mock setup on protected method #312

Closed
paulomorgado opened this issue Dec 26, 2016 · 2 comments

Comments

@paulomorgado
Copy link

Version: 4.6.38-alpha
Project: .NET Core class library

I have this test that works just fine:

[Fact]
public async Task EventStateBaseWithoutEventTransitions_WithoutCancellation_ReturnsReturnValueOfExecuteEventStepAsync()
{
    var logger = new TestLogger();

    var stateTransition = new Transition("Targeted", Mock.Of<TransitionTarget>(), logger.TransitionAction, null);

    var stateMock = new Mock<EventStateBase>(
        "test",
        logger.StateEnterAction,
        logger.StateExitAction,
        logger.StateCancelledAction)
    {
        CallBase = true,
    };

    stateMock.Protected()
        .Setup<Task<Transition>>("ExecuteEventStepAsync", It.IsAny<CancellationToken>())
        .ReturnsAsync(stateTransition);

    var actual = await stateMock.Object.ExecuteAsync(CancellationToken.None);

    Assert.Equal(stateTransition, actual);
    Assert.Equal(">test;<test;", logger.ToString());

    stateMock.Protected().Verify<Task<Transition>>("ExecuteEventStepAsync", Times.Once(), It.IsAny<CancellationToken>());
}

But if I add a call to a public method of the mocked instance, the setup is ignored and the mock will return the default value and, on verification, is reported as not having been called.

It happens with this test, for example:

[Fact]
public async Task EventStateBaseWithEventTransitions_WithoutCancellationWithoutTriggeredEventTransitionAndStateExecutionReturningNull_DoesntCompleteExecution()
{
    var logger = new TestLogger();

    var eventName = "event";

    var eventTransition = new Transition("Event", Mock.Of<TransitionTarget>(), logger.TransitionAction, null);

    var stateMock = new Mock<EventStateBase>(
        "test",
        logger.StateEnterAction,
        logger.StateExitAction,
        logger.StateCancelledAction)
    {
        CallBase = true,
    };

    stateMock.Protected()
        .Setup<Task<Transition>>("ExecuteEventStepAsync", It.IsAny<CancellationToken>())
        .ReturnsAsync(null);

    stateMock.Object.AddEventTransition(eventName, eventTransition);

    var executeTask = stateMock.Object.ExecuteAsync(CancellationToken.None);

    Assert.False(executeTask.IsCompleted);
    Assert.Equal(">test;", logger.ToString());

    stateMock.Protected().Verify<Task<Transition>>("ExecuteEventStepAsync", Times.Once(), It.IsAny<CancellationToken>());
}

What am I doing wrong?

@stakx
Copy link
Contributor

stakx commented Jun 21, 2017

Could you please provide a short but complete repro code? It would be easier to look into this if one didn't have to guess at all the types and symbols not defined here. Thank you!

@stakx
Copy link
Contributor

stakx commented Jul 4, 2017

Closing for now due to inactivity. @paulomorgado, if you'd like to continue this, just report back with complete repro code and we can reopen.

@stakx stakx closed this as completed Jul 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants