question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocks with the same verifiable invocation setup multiple times

See original GitHub issue
const m = Mock.ofInstance(a => { });
m.setup(x => x(It.isValue(0))).returns(() => 0).verifiable();
m.setup(x => x(It.isValue(0))).returns(() => 0).verifiable();
m.object(0);
m.object(0);

m.verifyAll();

This fails with Verify Exception: Expected invocation on the mock 2 times, invoked 1 times. Is this not a supported scenario? I have a test where I set up several invocations, and a couple of them end up with the same expected signature. Is it expected that I only register each one once, then verify that it was called the correct number of times?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
deneboultoncommented, Feb 7, 2018

Hi, would you be able to verify if multiple registered setups work with Mock.ofType<Interface> when the matcher is exactly the same, please? From the docs and the above example I expected that it would.

I have code that looks like this:

const mock = TypeMoq.Mock.ofType<MyInterface>();
// setup for first call
mock
    .setup(f => f.funcThatReturnsPromise())
    .returns(() => Promise.resolve(valueA));
// setup for second call
mock
    .setup(f => f.funcThatReturnsPromise())
    .returns(() => Promise.resolve(valueB));

What happens is that the second setup trumps the first; so the first call receives valueB but the second call receives undefined. I found it interesting that all subsequent calls after the first return undefined and not valueB - though naturally both of those outcomes are undesirable as we want callA -> valueA & callB -> valueB. I would only expected undefined to be returned after the second call.

If I remove the second setup then all calls to funcThatReturnsPromise returns valueA as expected.

Previously this test setup code differed in that the setups were both different because funcThatReturnsPromise was receiving an argument, so:

mock
    .setup(f => f.funcThatReturnsPromise(inputA))
    .returns(() => Promise.resolve(valueA));
mock
    .setup(f => f.funcThatReturnsPromise(inputB))
    .returns(() => Promise.resolve(valueB));

This worked fine, so a call using inputA returned valueA and so on.

Any help would be much appreciated, thanks.

0reactions
florinncommented, Jun 6, 2016

I guess it does make more sense to replay them in the order they were recorded. I’ve made the necessary changes.

Typemoq ver 0.1.1 is available and it includes these changes.

Please give it a try and let me know if you have any comments.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Moq - Is it possible to specify in a Setup the Verify criteria (e.g. ...
From what I can gather, Moq's Setup(SomeExpression).Verifiable() called along with Verify() , basically does a Verify(SomeExpression, Times.
Read more >
Mockito Verify - DigitalOcean
Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once,...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
In this article, we'll cover multiple mock interfaces, listening invocations, matchers, and argument captors, and see firsthand how Mockito makes your tests ...
Read more >
Mockito (Mockito 4.10.0 API) - javadoc.io
Verifying exact number of invocations / at least once / never ... is more important - when you stubbed the same method with...
Read more >
Verifications - Unit Testing in C#
If your unit test uses multiple mocks, you can use Mock.Verify to verify all verifiable configuration at once.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found