英文:
Cypress: assert argument of stubbed function with Regex
问题
cy
.get('@myMethod')
.should('be.calledWithMatch', 'start', {
segmentB: Cypress.sinon.match(/^MPI_/)
})
英文:
I have a stubbed method that is having the following structure printed in the Cypress console:
myMethod('start', Object{5})
I know that the object has a key, segmentB
-> when console logging it in the stub, I see it but I do not want to start making assertions in the stub
I want to assert, that the value of segmentB starts with 'MPI_'
I though of combining "should be called with match" and Cypress.sinon assertions like the following, but it is not working.
cy
.get('@myMethod')
.should('be.calledWithMatch', 'start', {
segmentB: Cypress.sinon.match(/^MPI_/)
})
.should('beCalledWithMatch', 'start')
or asserting key/value pairs of the objects without variable parts works, but I'd appreciate any help for asserting with a regex.
答案1
得分: 5
以下是您要翻译的内容:
"it works for me, here is a simple reproduction test that passes."
it('uses calledWithMatch assertion', () => {
const wrapper = {
myMethod: function (param1, param2) {
console.log('Called with', param1, param2)
}
}
cy.spy(wrapper, 'myMethod').as('myMethod')
wrapper.myMethod('start', {segmentB: 'MPI_abc'})
cy.get('@myMethod')
.should('be.calledWithMatch', 'start', {
segmentB: Cypress.sinon.match(/^MPI_/) // ✅ passes
})
})
英文:
It works for me, here is a simple reproduction test that passes.
it('uses calledWithMatch assertion', () => {
const wrapper = {
myMethod: function (param1, param2) {
console.log('Called with ', param1, param2)
}
}
cy.spy(wrapper, 'myMethod').as('myMethod')
wrapper.myMethod('start', {segmentB: 'MPI_abc'})
cy.get('@myMethod')
.should('be.calledWithMatch', 'start', {
segmentB: Cypress.sinon.match(/^MPI_/) // ✅ passes
})
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论