devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.94k stars 802 forks source link

Expected invocation on the mock should never have been performed, but was 1 times. when upgrading to the latest moq version 4.16.1 from 4.10.0 #1168

Closed aniketiitg closed 3 years ago

aniketiitg commented 3 years ago

I faced a similar issue when upgrading to the latest moq version 4.16.1 from 4.10.0

Earlier - the verify() of a method with "It.IsAny\<string>()", when invoked with parameter null -> This scenario was considered Negative. serviceMock.Verify(x => x.SomeMethod(<It.IsAny\<string>()), Times.Never) passes when - SomeMethod(null)

Now with the current version 4.16.1, passing null, the verify considers it as positive. SomeMethod(null) ->Expected invocation on the mock should never have been performed, but was 1 times.

@stakx - any idea related to this would help.

Originally posted by @aniketiitg in https://github.com/moq/moq4/issues/1055#issuecomment-854501616

stakx commented 3 years ago

Test code:

using Moq;

using Xunit;

public class Tests
{
    [Fact]
    public void Test()
    {
        var serviceMock = new Mock<IService>();
        serviceMock.Object.SomeMethod(null);
        serviceMock.Verify(x => x.SomeMethod(It.IsAny<string>()), Times.Never);
    }
}

public interface IService
{
    void SomeMethod(string str);
}

Outcome with Moq 4.10.0

Moq.MockException : 
Expected invocation on the mock should never have been performed, but was 1 times: x => x.SomeMethod(It.IsAny<String>())
No setups configured.

Performed invocations: 
IService.SomeMethod(null)

Outcome with Moq 4.16.1

Moq.MockException : 
Expected invocation on the mock should never have been performed, but was 1 times: x => x.SomeMethod(It.IsAny<string>())

Performed invocations:

   Mock<IService:1> (x):

      IService.SomeMethod(null)

Conclusion

While the error message format has changed a little, I cannot confirm your claim that there has been a fundamental change in behavior between versions 4.10.0 and 4.16.1 regarding whether or not null matches It.IsAny<>().

What am I missing?

stakx commented 3 years ago

Closing due to missing feedback. We cannot proceed without a repro.