Cypress:使用正则表达式断言存根函数的参数

huangapple go评论62阅读模式
英文:

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
  })
})

huangapple
  • 本文由 发表于 2023年3月1日 15:36:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定