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

Times.Never is incorrectly reported in a setup with Times.Exactly #211

Closed
kkm000 opened this issue Oct 26, 2015 · 8 comments
Closed

Times.Never is incorrectly reported in a setup with Times.Exactly #211

kkm000 opened this issue Oct 26, 2015 · 8 comments
Labels

Comments

@kkm000
Copy link

kkm000 commented Oct 26, 2015

The following calll to Verify

      mock.Verify(x => x.Create(It.IsAny<CspParameters>()), Times.Exactly(11);

results in this message:

Moq.MockException : 
Expected invocation on the mock exactly 11 times, but was 1 times: x => x.Create(It.IsAny<CspParameters>())

Configured setups:
x => x.Create(It.IsAny<CspParameters>()), Times.Never

Performed invocations:
....

Note misreported Times.Never. Expected in the message either Times.Exactly(11), or a message to the effect of absence of any configured setups, because there were truly none. The number is reported correctly in the preceding text however.

@LeonidLevin
Copy link

I cannot reproduce this issue. This is my test:

            [Fact]
            public void IfNoSetupsAndTestFails_ReportNoConfiguredSetups()
            {
                var mock = new Mock<ISimpleInterface>();

                var a = new MyClass();

                mock.Object.Method(a);

                mock.Verify(x => x.Method(It.IsAny<MyClass>()), Times.Exactly(11));
            }

            public interface ISimpleInterface
            {
                int Method(MyClass a);
            }

            public class MyClass { }

The test results are as follows:

Moq.MockException : 
Expected invocation on the mock exactly 11 times, but was 1 times: x => x.Method(It.IsAny<MyClass>())
No setups configured.

The "No setups configured" message appears as there were no setups.

@kkm000
Copy link
Author

kkm000 commented May 25, 2016

Has a new version been out since? This bug turns 7 months tomorrow...

@kzu
Copy link
Member

kzu commented May 25, 2016

You bet :p

On Tue, May 24, 2016, 10:19 PM Kirill Katsnelson notifications@github.com
wrote:

Has a new version been out since? This bug turns 7 months tomorrow...


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#211 (comment)

@stakx
Copy link
Contributor

stakx commented Jun 4, 2017

@kkm000, it's been a while, but if you are still willing to help diagnose this error, could you please provide a small but complete program or unit test that reproduces the problem?

@kkm000
Copy link
Author

kkm000 commented Jun 7, 2017

@stakx: Let me try tomorrow, thanks! It's been a while, so the bug might have fixed itself since then. I'll test.

@stakx
Copy link
Contributor

stakx commented Jun 25, 2017

This could be fixed (the bug sits in Mock.FormatCallCount called from Mock.FormatSetupsInfo), but whether it's actually worth fixing depends on how #373 will proceed:

  • The ability to specify the expected number of calls during setup has been deprecated long ago, so perhaps that bit of information should probably also be removed from the exception message.

  • However, if the ability to specify Times during setup makes it back into Moq (which is what Set Times expectation on Setup #373 aims for), then this error here needs to be fixed as well.

@stakx stakx added the bug label Jun 25, 2017
@stakx
Copy link
Contributor

stakx commented Jul 13, 2017

Here's a repro for this (or at least for a very similar) issue:

var mock = new Mock<IX>(MockBehavior.Never);
mock.Setup(x => x.Create(It.IsAny<CspParameters>()));
mock.Object.Create(null);
mock.Object.Create(null);
mock.Verify(x => x.Create(It.IsAny<CspParameters>()), Times.Exactly(11));

public interface IX
{
    void Create(CspParameters parameters);
}

public class CspParameters { }

Which produces the following exception message:

Unhandled Exception: Moq.MockException:
Expected invocation on the mock exactly 11 times, but was 2 times: x => x.Create(It.IsAny<CspParameters>())

Configured setups:
x => x.Create(It.IsAny<CspParameters>()), Times.Exactly(2)

Performed invocations:
IX.Create(null)
IX.Create(null)
   at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount)
   at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
   at Moq.Mock.Verify[T](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
   at Moq.Mock`1.Verify(Expression`1 expression, Times times)

Note how it misreports the number of actual invocations as the number of expected invocations in the section "Configured setups", Fact is that no Times has been configured with Setup at all.

@stakx
Copy link
Contributor

stakx commented Jul 13, 2017

This should be fixed with the next release of Moq (version >4.7.63).

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

No branches or pull requests

4 participants