基本上
expect()
在Cypress命令运行之前被执行。
您需要将expect链接到
.then()
根据
toggle-spec
实例
describe('Button', () => {
it('should call the onClick function', () => {
const onClick = cy.stub();
mount(<Button onClick={onClick} />);
cy.get(`[data-test-ref=button]`).click()
.then(() => {
expect(onClick).to.be.called; // succeeds
});
});
});
或者根据
stub
实例
describe('Button', () => {
it('should call the onClick function', () => {
const onClick = cy.stub().as('my-button');
mount(<Button onClick={onClick} />);
cy.get(`[data-test-ref=button]`).click();
cy.get('@my-button').should('have.been.called');
});
});