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

CallBase should not be allowed for delegate mocks #706

Closed
stakx opened this issue Oct 13, 2018 · 4 comments
Closed

CallBase should not be allowed for delegate mocks #706

stakx opened this issue Oct 13, 2018 · 4 comments

Comments

@stakx
Copy link
Contributor

stakx commented Oct 13, 2018

Reproduction code:

var mock = new Mock<Action>();
mock.Setup(m => m()).CallBase();
mock.Object.Invoke();

Actual behavior:

mock.Object.Invoke() throws a NotImplementedException with message:

This is a DynamicProxy2 error: The interceptor attempted to 'Proceed' for method 'Void Invoke()' which has no target. When calling method without target there is no implementation to 'proceed' to and it is the responsibility of the interceptor to mimic the implementation (set return value, out arguments etc)

Expected behavior:

The call to .CallBase() should throw a NotSupportedException with a message explaining that CallBase cannot be used with delegate mocks.

The same should happen when setting a delegate mock's CallBase property to true.

@m-wild
Copy link
Contributor

m-wild commented Oct 15, 2018

Hey, created PR #708

@stakx
Copy link
Contributor Author

stakx commented Oct 15, 2018

@tehmantra, thank you for picking this one up! 👍 I've posted a review on your PR.

(P.S.: On a totally unrelated note, please say hello to Auckland and the whole Hauraki Gulf for me. Wish I were there.)

@stakx stakx added this to the 4.10.1 milestone Oct 15, 2018
@viktor-nikolaev
Copy link

It looks like something is wrong with this issue.

I have this code

[Theory]
[AutoMoqData]
internal async Task GetAutomationStatistics_should_return_correct_statistics(
  [Frozen] Mock<GetAutomationsLogTotalCount> getAutomationsLogTotalCount,
  [Frozen] GetCurrentlyRunningSchedulesResult getCurrentlyRunningSchedulesResult,
  [Frozen] GetAutomationsTotalCountResult getAutomationsTotalCountResult,
  GetAutomationStatisticsQuery query,
  GetAutomationStatisticsHandler sut,
  CancellationToken ct
)
{
  getAutomationsLogTotalCount
    .Setup(_ => _(It.IsAny<GetAutomationsLogTotalCountQuery>(), ct))
    .ReturnsAsync((GetAutomationsLogTotalCountQuery x, CancellationToken _) =>
      new GetAutomationsLogTotalCountResult
      {
        TotalCount = (long) x.LogLevel.GetValueOrDefault()
      }
    );

  var expectedResult = new GetAutomationStatisticsResult
  {
    AutomationsRunningNow = await getCurrentlyRunningSchedulesResult.Items.LongCount(ct),
    TotalAutomations = getAutomationsTotalCountResult.TotalCount,
    ErrorsReported = (long) EapLogLevel.Error,
    WarningsReported = (long) EapLogLevel.Warn,
  };

  var result = await sut.Handle(query, ct);

  result.Should().BeEquivalentTo(expectedResult);
}

Where GetAutomationsLogTotalCount is a delegate. And I got the exception

AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from Moq.Mock`1[AutomationsWeb.Features.Statistics.GetAutomationsLogTotalCount] because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

Request path:
	Moq.Mock`1[AutomationsWeb.Features.Statistics.GetAutomationsLogTotalCount] getAutomationsLogTotalCount
	  Moq.Mock`1[AutomationsWeb.Features.Statistics.GetAutomationsLogTotalCount]

Inner exception messages:
	System.NotSupportedException: CallBase cannot be used with Delegate mocks.

But I do not call CallBase method.

zvirja pushed a commit to AutoFixture/AutoFixture that referenced this issue Jan 26, 2019
There was a breaking change in Moq 4.10.1 (see devlooped/moq#706)
and now `CallBase` flag is no longer supported for the delegates.
The unit tests do not reflect that somehow, as currently 
we don't have a way to run tests again multiple minor versions
of the same dependency.
@zvirja
Copy link

zvirja commented Jan 26, 2019

@Veikedo Notice, it has been fixed in AutoFixture. Please update to a new version (it will be published soon) to get a fix 😉

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

4 participants