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

Mock throws exception when called twice #2046

Open
nathanlepori opened this issue Jun 15, 2019 · 5 comments
Open

Mock throws exception when called twice #2046

nathanlepori opened this issue Jun 15, 2019 · 5 comments
Labels

Comments

@nathanlepori
Copy link

Description
Mock throws ExpectationError: Anonymous mock already called once when called twice, even with two preprogrammed returns()/resolves().

To Reproduce

const myMock = sinon.mock()
    .returns('something')
    .returns('something else');

myMock();
myMock();    // Throws 'ExpectationError: Anonymous mock already called once'

Expected behavior
Mock should be able to be reusable, as far as I know. It would be useful in case the system under test calls the function multiple times. I know this works with stubs.

Context

  • Library version: 7.0.13
  • Environment: Node 12.0.0
  • Operating System: Windows 10
@fatso83
Copy link
Contributor

fatso83 commented Jun 26, 2019

Not sure why anonymous mocks differ from the ones on objects, but adding atLeast() works:

https://runkit.com/fatso83/sinon-workbook/1.0.0

@stale
Copy link

stale bot commented Aug 25, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 25, 2019
@stale stale bot removed the stale label Aug 26, 2019
@mroderick
Copy link
Member

This looks like it need a bit more investigation

@rgroothuijsen
Copy link
Contributor

For anonymous mocks, mock delegates most of its behavior to stub (including returns) while maintaining its own minCalls and maxCalls counters that never get updated after calling returns. As noted, these counters can be updated by explicitly calling atLeast and atMost.

@sinonjs sinonjs deleted a comment from Jhoem Sep 15, 2019
@fatso83
Copy link
Contributor

fatso83 commented Oct 9, 2019

I think this should be closed or changed to update the docs. I guess it's not stated explicitly.

Also, we talked about removing mocks for Sinon 2 and splitting it out into a separate side-project, seeing that none of the maintainers care about them or use them, and seeing we really don't know enough about how they work these days, maybe it's time to revisit that idea 😄

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