用 sinon 存根,用 chai 测试

Posted

技术标签:

【中文标题】用 sinon 存根,用 chai 测试【英文标题】:Stubbing with sinon and testing with chai 【发布时间】:2021-12-10 04:19:08 【问题描述】:

我正在学习使用 Mocha 和 Chai 进行测试,并且有一个关于存根函数的问题。我正在尝试对映射数组并以 CSV 格式返回它的函数进行存根。但是,我的测试失败并显示以下消息:

ReferenceError: firstName 未定义

我理解的意思是测试找不到它,但我真的不明白为什么,因为我觉得我已经声明了它?任何帮助或指出我正确的方向都会有所帮助。我的代码和测试将在下面:

export default (
  teamId,
  allApproverUserItemsForTeam = defaultAllApproverUserItemsForTeam,
) => 
  const teamApprovers = allApproverUserItemsForTeam( teamId );

  const csvContent = teamApprovers.map(teamApprover =>
    `$teamApprover.firstName, $teamApprover.lastName, $teamApprover.emailAddress`,
  );

  const joinedApproversList = csvContent.join();

  return joinedApproversList;
;
describe('create_team_approvers_csv_test', () => 
  describe('given a teamId for a team that has a list of approvers', () => 
    const teamId = randoms.randomId();
    const allApproverUserItemsForTeam = sinon.stub();
    const approversForTeam = [
      
        id: 'fwwfw',
        emailAddress: 'joe@bloggs.com',
        firstName: 'Joe',
        lastName: 'Bloggs',
        title: 'Mr',
        isTeamProfile: false,
        version: 1,
      ,
      
        id: 'wgerher6446',
        emailAddress: 'jane@doe.com',
        firstName: 'Jane',
        lastName: 'Doe',
        title: 'Mrs',
        isTeamProfile: false,
        version: 3,
      ,
    ];
    allApproverUserItemsForTeam.withArgs( teamId ).returns(approversForTeam);

    it('should create a list of approvers in a required CSV format', () => 
      const expected = 
        firstName,
        lastName,
        emailAddress,
      ;

      const result = createTeamApproversCsv( teamId );

      expect(result).to.be.deep.equal(expected);
    );
  );
);

【问题讨论】:

【参考方案1】:

也许这是一个错字。您没有将存根的 allApproverUserItemsForTeam 函数传递给 createTeamApproversCsv 函数。

例如

index.ts:

//@ts-nocheck
const defaultAllApproverUserItemsForTeam = ( teamId ) => 
  return [];
;

export default ( teamId, allApproverUserItemsForTeam = defaultAllApproverUserItemsForTeam ) => 
  const teamApprovers = allApproverUserItemsForTeam( teamId );

  const csvContent = teamApprovers.map(
    (teamApprover) => `$teamApprover.firstName, $teamApprover.lastName, $teamApprover.emailAddress`,
  );

  const joinedApproversList = csvContent.join();

  return joinedApproversList;
;

index.test.ts:

import createTeamApproversCsv from '.';
import  expect  from 'chai';
import sinon from 'sinon';

describe('create_team_approvers_csv_test', () => 
  describe('given a teamId for a team that has a list of approvers', () => 
    const teamId = '123';
    const allApproverUserItemsForTeam = sinon.stub();
    const approversForTeam = [
      
        id: 'fwwfw',
        emailAddress: 'joe@bloggs.com',
        firstName: 'Joe',
        lastName: 'Bloggs',
        title: 'Mr',
        isTeamProfile: false,
        version: 1,
      ,
      
        id: 'wgerher6446',
        emailAddress: 'jane@doe.com',
        firstName: 'Jane',
        lastName: 'Doe',
        title: 'Mrs',
        isTeamProfile: false,
        version: 3,
      ,
    ];
    allApproverUserItemsForTeam.withArgs( teamId ).returns(approversForTeam);

    it('should create a list of approvers in a required CSV format', () => 
      const expected = ['Joe, Bloggs, joe@bloggs.com', 'Jane, Doe, jane@doe.com'].join();

      const result = createTeamApproversCsv( teamId, allApproverUserItemsForTeam );

      expect(result).to.be.deep.equal(expected);
    );
  );
);

测试结果:

create_team_approvers_csv_test
    given a teamId for a team that has a list of approvers
      ✓ should create a list of approvers in a required CSV format


  1 passing (5ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |    87.5 |        0 |   66.67 |    87.5 |                   
 index.ts |    87.5 |        0 |   66.67 |    87.5 | 3                 
----------|---------|----------|---------|---------|-------------------

【讨论】:

谢谢@slideshowp2 在您的帮助下,我现在通过了测试。你是对的,我没有将存根函数传递给 createTeamApproversCsv 函数。这么小的错误,谢谢指出。

以上是关于用 sinon 存根,用 chai 测试的主要内容,如果未能解决你的问题,请参考以下文章

markdown 节点单元测试备忘单:Mocha,Chai和Sinon

markdown Mocha,Chai和Sinon的终极单元测试作弊表

markdown Mocha,Chai和Sinon的终极单元测试作弊表

Cypress系列(12)- Cypress 编写和组织测试用例篇 之 断言

Cypress系列(12)- Cypress 编写和组织测试用例篇 之 断言

用 sinon 模拟/存根猫鼬 findById