我对a custom Cypress command of mine进行了以下测试:
it('can navigate to a url', () => {
const history = createHistory();
cy.window().then(win => ((win as any).__chh__history__ = history));
cy.spy(history, 'push');
history.listen((location, action) => {
expect(action).to.equal('PUSH');
expect(location.pathname).to.equal(PATH);
expect(location.state).to.equal('foobar');
});
cy.navigate(PATH, 'foobar');
cy.wait(0).then(() => {
expect(history.push).to.have.been.called;
});
});
该命令运行正常,但是如果我删除cy.wait(0)
,仅执行expect
,则测试将失败。
我认为是因为cy.navigate
命令(通过Cypress.Commands.add
添加)已排队,就像其他赛普拉斯命令一样,这意味着expect
实际上在任何赛普拉斯命令还没有启动之前就已经运行了吗?
如果是这样,那很好,这当然是您所期望的,但是赛普拉斯测试中是否有一种方法可以在不使用cy.wait(0)
的情况下将事物排队?
例如,当我检查元素时,我可以使用cy.get(something).should($el => expect(...))
,但是在这种情况下,没有什么可以“上”的……我当然可以cy.get('body')
或其他任何东西,但是我在这种情况下,真的不想要任何东西。 ?
答案 0 :(得分:0)
docs显示了在间谍建立之后使用裸露的expect()
cy.spy(util, 'addListeners')
App.start()
expect(util.addListeners).to.be.called
但是我认为这会产生误导,因为expect()
立即运行,但是中间的赛普拉斯命令可能会固有的延迟或束缚,并且不能保证在执行期望之前就完成。
另外两种测试方法是:
确保期望仅在您的自定义命令之后运行
cy.navigate(PATH, 'foobar').then(() => {
expect(history.push).to.have.been.called;
})
将期望值放入赛普拉斯命令队列中
cy.navigate(PATH, 'foobar');
cy.wrap(history).its('push').should('have.been.called')