简体   繁体   English

用 sinon 存根并用 chai 测试

[英]Stubbing with sinon and testing with chai

I am in the process of learning testing with Mocha and Chai, and have a question about stubbing a function.我正在用 Mocha 和 Chai 学习测试,有一个关于 stubbing 函数的问题。 I am trying to stub a function that maps an array and returns it in a CSV format.我正在尝试存根一个映射数组并以 CSV 格式返回它的函数。 However, my tests are failing with the following message:但是,我的测试失败并显示以下消息:

ReferenceError: firstName is not defined参考错误:名字未定义

Which I understand means that the test can't find it but I don't really understand why as I feel like I have declared it?我理解这意味着测试找不到它,但我真的不明白为什么,因为我觉得我已经声明了它? Any help or pointing me in the right direction would be helpful.任何帮助或将我指向正确的方向都会有所帮助。 My code and test will be underneath:我的代码和测试将在下面:

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

Maybe it's a typo.也许这是一个错字。 You didn't pass the stubbed allApproverUserItemsForTeam function to createTeamApproversCsv function.您没有将存根allApproverUserItemsForTeam函数传递给createTeamApproversCsv函数。

Eg例如

index.ts : 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 : 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);
    });
  });
});

test result:测试结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM