Cypress 断言请求';没有发生
我有以下测试Cypress 断言请求';没有发生,cypress,Cypress,我有以下测试 it('does not trigger request', () => { cy.shortcutThatTriggersRequest() cy.route({ method: 'POST', url: '[url]', onRequest: () => { cy.contains('request was invoked').should('exist') }, })
it('does not trigger request', () => {
cy.shortcutThatTriggersRequest()
cy.route({
method: 'POST',
url: '[url]',
onRequest: () => {
cy.contains('request was invoked').should('exist')
},
})
cy.wait(1000)
})
这就是我想要做的——如果给定的请求发生了,它将无法通过测试,如果没有,那么测试将通过
不过,它有两个麻烦的地方:
- 我发现,
;我不能忽略Cypress检测到您在调用某个命令中的一个或多个cy命令时,从该命令返回了一个承诺。
,否则测试将被标记为通过(在解决请求时测试结束,测试被标记为通过,然后失败);只有在发出请求时才会发生这种情况(因为wait
没有被触发,所以没有路由
可以在包含
的同一点触发)等待
- 我依靠
将测试标记为失败。我不想那样做;虽然这样的字符串永远不会存在,但它是一种变通方法;理想情况下,我希望cocy.contains('request was called').should('exist')
,但我看不到这种方法cy.fail()
- 好的,为了实现这一点,我使用了以下方法:
it.skip('does not trigger products requests', () => {
Cypress.on('fail', (error, runnable) => {
if (error.message.indexOf('Timed out retrying') !== 0) throw error
})
cy.server()
cy.route({
method: 'POST',
url: '[url]',
}).as('products')
cy.shortcutThatTriggersRequest()
cy.wait('@products', {
requestTimeout: 1000,
}).then((xhr) => {
assert.isNull(xhr.response.body)
})
})
如果达到超时,它将在失败
内处理,因此测试将通过。如果它进入内部,则测试将失败
这里还有一个问题,即如果检查了多个路由(cy.wait['route1',route2']
),但其中一个路由不存在,那么即使其中一个断言指定它不应该存在,测试也会通过,例如:
it.skip('does not trigger products requests', () => {
Cypress.on('fail', (error, runnable) => {
if (error.message.indexOf('Timed out retrying') !== 0) throw error
})
cy.server()
cy.route({
method: 'POST',
url: '[url]',
}).as('route1')
cy.route({
method: 'POST',
url: '[url]',
}).as('route2')
cy.shortcutThatTriggersRequest()
cy.wait(['@route1', '@route2'], {
requestTimeout: 1000,
}).then((xhrs) => {
assert.isNull(xhrs[0].response.body)
assert.isNull(xhrs[1].response.body)
})
})
即使有对其中一个端点的请求(我假设这是由处理onfail
引起的错误)。需要做的是,请求断言必须是连续的:
cy.wait('@route1', {
requestTimeout: 1000,
}).then((xhr) => {
assert.isNull(xhr.response.body)
})
cy.wait('@route2', {
requestTimeout: 1000,
}).then((xhr) => {
assert.isNull(xhr.response.body)
})
据我所知,我们无法在onfail
中检查error
的类型,因为它总是CypressError
不管是什么原因造成的,好的,为了实现这一点,我使用了以下方法:
it.skip('does not trigger products requests', () => {
Cypress.on('fail', (error, runnable) => {
if (error.message.indexOf('Timed out retrying') !== 0) throw error
})
cy.server()
cy.route({
method: 'POST',
url: '[url]',
}).as('products')
cy.shortcutThatTriggersRequest()
cy.wait('@products', {
requestTimeout: 1000,
}).then((xhr) => {
assert.isNull(xhr.response.body)
})
})
如果达到超时,它将在失败
内处理,因此测试将通过。如果它在内,则
内,则测试将失败
这里还有一个问题,即如果检查了多个路由(cy.wait['route1',route2']
),但其中一个路由不存在,那么即使其中一个断言指定它不应该存在,测试也会通过,例如:
it.skip('does not trigger products requests', () => {
Cypress.on('fail', (error, runnable) => {
if (error.message.indexOf('Timed out retrying') !== 0) throw error
})
cy.server()
cy.route({
method: 'POST',
url: '[url]',
}).as('route1')
cy.route({
method: 'POST',
url: '[url]',
}).as('route2')
cy.shortcutThatTriggersRequest()
cy.wait(['@route1', '@route2'], {
requestTimeout: 1000,
}).then((xhrs) => {
assert.isNull(xhrs[0].response.body)
assert.isNull(xhrs[1].response.body)
})
})
即使有对其中一个端点的请求(我假设这是由处理onfail
引起的错误)。需要做的是,请求断言必须是连续的:
cy.wait('@route1', {
requestTimeout: 1000,
}).then((xhr) => {
assert.isNull(xhr.response.body)
})
cy.wait('@route2', {
requestTimeout: 1000,
}).then((xhr) => {
assert.isNull(xhr.response.body)
})
据我所知,我们无法在onfail
中检查error
的类型,因为它总是CypressError
而不管是什么原因造成的