我正在学习Mocha和Chai的测试过程中,并且有一个关于存根功能的问题。我试图存根一个函数,映射一个数组,并返回它在CSV格式。但是,我的测试失败,显示以下消息:
ReferenceError: firstName is not defined
我理解的意思是测试找不到它,但我真的不明白为什么我觉得我已经声明了它?任何帮助或指给我正确的方向都很有帮助。我的代码和测试将在下面:
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);
});
});
});
可能是打错了。您没有将存根的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
----------|---------|----------|---------|---------|-------------------