Javascript 如何在Cypress中测试以特定形状的对象作为参数调用的存根函数?

Javascript 如何在Cypress中测试以特定形状的对象作为参数调用的存根函数?,javascript,testing,cypress,Javascript,Testing,Cypress,如何测试以特定形状的对象作为参数调用存根函数 例如,我试着做一些类似的事情 cy.get('@submitStub').should('have.been.calledWithMatch', { dateRange: { startDate: `The actual value here doesn't matter`, endDate: '', } }); 当然,上面的代码并不像预期的那样工作。有人能帮忙吗?谢谢大家! 您可以执行以下操作: describe('tes

如何测试以特定形状的对象作为参数调用存根函数

例如,我试着做一些类似的事情

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;
    });
  });
});
你可以做:

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

意思是,你不关心开始日期是什么值,只是它被定义了?@alliden是的,没错。意思是,你不关心开始日期是什么值,只是它被定义了?@alliden是的,没错。