sinonjs / sinonjs/sinon

Mock throws exception when called twice

Open
#2,046 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pinned
Dominant language
JavaScript
Stars
9.8k
Forks
809
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the mock implementation and its existing tests, then reproduce the two chained returns() calls from the issue. Confirm that invoking the mock twice returns the two configured values without an ExpectationError, and add or update coverage for repeated calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.