使用 cypress.io 进行测试时,是否有一种好方法可以断言没有对给定 URL 发出 XHR 请求?
我想断言单击“保存”按钮时会添加一个新的 foo,但单击“取消”按钮时不会添加新的 foo。
像这样的东西:
cy.route('POST', '/foos').as('postFoo');
cy.get('.cancel-button').click();
cy.route('@postFoo').should('not.have.been.called'); // (magic imaginary syntax!)
我已经尝试使用执行 assert.fail 的 onRequest 回调设置 cy.route,但是当调用 URL 时,测试并没有失败。
现在,我正在捕捉我的(有意的)“未发生请求”错误,如下所示:
cy.on('fail', (err, runnable) => {
expect(err.message).to.include('No request ever occurred.');
return false;
});
cy.wait('@postFoo', { timeout: 0 }).then(xhr => {
throw new Error('Unexpected API call.');
});
...这似乎有效,但肯定感觉不是很“柏树”。