在我正在开发的应用程序中,我使用 setInterval 和clearInterval。我想监视 clearInterval 以便查看该方法是否被调用。这基本上就是我正在做的事情
beforeEach(() => {
cy.clock(new Date())
})
it('Test Scenario' => {
const fn = cy.spy(document.defaultView, 'clearInterval')
//also tried fn = cy.spy(window, 'clearInterval')
...
...
...
//The clearInterval function is called upon entering the
//`then` part but the stub is reporting it hasn't been called
cy.tick(30000).then(() => {
expect(fn).to.have.been.calledOnce
})
})
afterEach(() => {
cy.clock().invoke('restore')
})
当我期望上面代码片段中的期望能够通过时,它却失败了。启动间谍的逻辑是否有效|正确?非常感谢您对上述问题的任何帮助。
Cypress 使用不同的
window
进行测试和应用程序。 cy.spy(window...
正在使用测试窗口,但要监视应用程序窗口,您将需要 cy.window()
命令。
let spy;
cy.window().then(appWindow => {
spy = cy.spy(appWindow, 'clearInterval')
})
... later
expect(spy).to.have.been.calledOnce
但是可能还有另一个复杂的情况,因为
cy.clock()
将clearInterval()
放在代理中,这样它就可以控制应用程序的计时功能,所以你可能无法监视它。
如果还是不行,抓一下
cy.clock()
的返回值,看看是否有调用信息。
let clock;
beforeEach(() => {
clock = cy.clock(new Date())
})
或者指定
clearInterval
不应被代理
beforeEach(() => {
cy.clock(new Date(), ['Date', 'setInterval']) // only proxy Date and setInterval
})