You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress中onCall()断言支持未文档化,请问确切支持哪些断言?

Clarifying Cypress's Supported Assertions & Sinon's onCall() Method

Great question! Let’s clear up the confusion between Sinon methods and Cypress-supported assertions, since that’s where the mix-up is happening.

First: onCall() isn’t an assertion—it’s a Sinon method

The onCall() you saw in the Cypress example is part of Sinon.js, not an assertion. Cypress actually bundles Sinon directly into its environment, so you get full access to all of Sinon’s stub, spy, and mock functionality. That includes methods like:

  • onCall(n): Defines behavior for the nth call to a stub/spy
  • returns(value): Sets a stub’s return value
  • throws(error): Makes a stub throw an error when called
  • callThrough(): Lets a stub call the original function

These are used to set up test doubles (stubs/spies) before you run assertions—they’re not part of the "assertions" list in the Cypress docs.

What Assertions Does Cypress Actually Support?

Cypress builds on top of Chai and adds several assertion libraries to cover different testing needs. Here’s the full breakdown:

1. Core Chai Assertions

These are the basic Chai assertions you’d use for general value checks:

  • expect(1 + 1).to.equal(2)
  • expect('hello').to.include('ell')
  • expect({ foo: 'bar' }).to.have.property('foo')

2. sinon-chai Assertions

This is the integration between Sinon and Chai that the Cypress docs mention. These assertions let you verify how your Sinon spies/stubs were called:

  • expect(mySpy).to.have.been.called
  • expect(myStub).to.have.been.calledWith('user-id-123')
  • expect(apiSpy).to.have.callCount(3)
  • expect(formSubmitStub).to.have.been.calledOnceWithExactly({ name: 'Jane' })

3. Chai-jQuery Assertions

Built for testing DOM elements, these let you make assertions about elements’ state or content:

  • expect($('.submit-btn')).to.be.disabled
  • expect($('#modal')).to.be.visible
  • expect($('.user-card')).to.contain('John Doe')
  • expect($('input[name="email"]')).to.have.value('john@example.com')

4. Chai-as-Promised Assertions

For testing asynchronous Promise-based code:

  • expect(fetchUser()).to.eventually.have.property('id', 456)
  • expect(rejectingPromise()).to.be.rejectedWith('Invalid credentials')

Quick Example to Tie It All Together

Here’s how you’d use onCall() (a Sinon method) alongside a sinon-chai assertion in Cypress:

// Set up a stub with onCall() to return different values on each call
const getDataStub = cy.stub(window.api, 'getData')
  .onCall(0).returns({ data: 'first load' })
  .onCall(1).returns({ data: 'second load' })

// Trigger the calls
cy.get('#refresh-btn').click()
cy.get('#refresh-btn').click()

// Use a sinon-chai assertion to verify the stub was called twice
expect(getDataStub).to.have.been.calledTwice

To sum up: Cypress supports all Sinon’s core functionality for creating stubs/spies, and its official assertion suite includes core Chai, sinon-chai, Chai-jQuery, and Chai-as-Promised. The onCall() method falls under Sinon’s setup tools, not the assertion category, which is why it’s not listed in the docs’ assertion section.

内容的提问来源于stack exchange,提问作者musicformellons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:12:39