簡體   English   中英

如何在 Cypress 中測試以 object 的特定形狀調用的存根 function 作為參數?

[英]How to test a stubbed function called with certain shape of object as argument in Cypress?

如何測試以某種形狀的 object 作為參數調用存根 function ?

例如,我試圖做類似的事情

cy.get('@submitStub').should('have.been.calledWithMatch', {
  dateRange: {
    startDate: `The actual value here doesn't matter`,
    endDate: '',
  }
});

當然,上面的代碼不能按預期工作。 任何人都可以幫忙嗎? 謝謝!

你可以做:

describe('test', () => {
  it('test', () => {
    const obj = {
      method () {}
    };
    cy.stub(obj, 'method').as('stubbed')

    obj.method({
      dateRange: {
        startDate: `The actual value here doesn't matter`,
        endDate: '',
      }
    });

    const isNotUndefined = Cypress.sinon.match( val => {
      return val !== undefined;
    });

    cy.get('@stubbed').should('have.been.calledWithMatch', {
      dateRange: {
        startDate: isNotUndefined,
        endDate: isNotUndefined,
      }
    });

    // or
    cy.get('@stubbed').should('have.been.calledWithMatch', {
      dateRange: Cypress.sinon.match( obj => {
        return obj &&
          'startDate' in obj &&
          'endDate' in obj
      })
    });

    // or
    cy.get('@stubbed').should('have.been.calledWithMatch', {
      dateRange: Cypress.sinon.match.has("startDate")
        .and(Cypress.sinon.match.has("endDate"))
    });

    // or
    cy.get('@stubbed').should('have.been.calledWithMatch', arg1 => {
      // do whatever comparisons you want on the arg1, and return `true` if 
      //  it matches
      return true;
    });
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM