Skip to content

Fix mock onCall behavior for matching expectations#2717

Open
puneetdixit200 wants to merge 1 commit into
sinonjs:mainfrom
puneetdixit200:fix/mock-callsfake-return-values
Open

Fix mock onCall behavior for matching expectations#2717
puneetdixit200 wants to merge 1 commit into
sinonjs:mainfrom
puneetdixit200:fix/mock-callsfake-return-values

Conversation

@puneetdixit200
Copy link
Copy Markdown

Purpose (TL;DR) - mandatory

Fixes #2663 by making ordered mock expectations use the method-level call index when selecting onCall() behavior.

Background (Problem in detail) - optional

When separate expectations are configured for the same mocked method, each expectation only sees its own call count. That meant onCall(1) or onCall(2) on later matching expectations fell back to default behavior, so callsFake() returned undefined.

Solution - optional

invokeMethod() now computes the current call index across all expectations for the mocked method and passes it to the selected expectation for the duration of the invocation. getCurrentBehavior() uses that override when present, otherwise it keeps the existing per-stub call-count behavior.

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. Run:
    • npx mocha --recursive -R dot test/src/mock-test.js --grep "returns callsFake values"
    • npx mocha --recursive -R dot test/src/mock-test.js
    • npx mocha --recursive -R dot "test/src/**/*-test.js" --timeout 10000
    • npm run lint
    • npx prettier --check src/sinon/mock.js src/sinon/stub.js test/src/mock-test.js
    • git diff --check

Not run locally: npm test because npm run build-artifacts currently fails on Windows before tests with Entry module "src\create-sinon-api.js" cannot be external.

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

callsFake() on mocks only returns a value the first time

1 participant