我正在尝试为我们的小型但很快会增长的 testcafe 业务功能库编写单元测试 - 即使用页面文件的库,以测试任何非简单逻辑。
为此,我正在尝试为此功能 entityTabs 编写我的第一个单元测试——它是一个导入的异步模块,ui 是一个导入的非异步模块,accountAssignmentControls 是一个页面文件。这提到的那些功能。在模块中进一步定义。t 是一个 testcafe 导入。
最初我想删除所有函数并测试一次调用 statAccountAssignments (之后我将测试传入的数据的各种组合,但我无法超越第一个障碍)。
我 UNIT 测试了我们的非异步无头测试库,但是
- a) 那是非异步的并且
- b)它是较旧的节点,因此某些结构等有所不同。
其中一位开发人员使用 chair、mocha 和 sinon 添加了一些单元测试——但这些是针对项目的非异步部分(主要是文件处理),所以我无法得到他写的应用。
FUT
async assignAccounts(assignments) {
await entityTabs.startAccountAssignment();
ui.logWithTimeStamp('WAiting for Account assignment dialog to appear', 2);
await t
.expect(accountAssignmentControls.accountAssignmentHeader.innerText)
.contains('Account Assignment', 'Header contains Account Assignment');
ui.logWithTimeStamp('Header visible waiting for accounts list', 2);
await this.checkAccountList();
const assignmentsCount = assignments.length;
ui.logWithTimeStamp('Doing ' + assignmentsCount + ' assignments: ', 1);
for (let i=0; i<assignmentsCount; i++) {
ui.logWithTimeStamp(i + ': ' + JSON.stringify(assignments[i]), 1);
if (assignments[i].type === 'credit') {
await this.assignCreditAccount(assignments[i]);
} else if (assignments[i].type === 'debit') {
await this.assignDebitAccount(assignments[i]);
} else {
await this.assignFullAccount(assignments[i]);
}
ui.logWithTimeStamp('Assignment done', 3);
await t.takeScreenshot();
};
}
当前失败的测试
import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import AccountAssignment from '../../business-functions/account-assignment';
let accountAssignmentControls;
// let busyLoader;
let entityTabs;
let ui;
let t;
// Register the sinon-chai extensions.
chai.use(sinonChai);
const expect = chai.expect;
// We must turn off the no-invalid-this rule because of how mocha uses this to be the current test fixture.
/* eslint no-invalid-this: "off" */
describe('business-functions - Account assignment', function() {
describe('AccountAssignment class', function() {
describe('assignAccounts', function() {
let sandbox;
before(() => {
sandbox = sinon.sandbox.create();
});
after(() => {
sandbox.restore();
});
describe('when one of each assignment', function() {
it('starts assignment', async function() {
const fakeResponse = 'dont care';
const accountAssignment = new AccountAssignment();
t = sinon.stub();
entityTabs = sinon.stub();
ui = sinon.stub();
ui.logWithTimeStamp = sinon.stub();
accountAssignmentControls = sinon.stub();
// let f = sinon.fake();
let startAccountAssignment = sandbox
.stub(entityTabs, 'startAccountAssignment')
.resolves(fakeResponse);
// sandbox.stub(t, 'expect');
sandbox
.stub(accountAssignment, 'checkAccountList')
.resolves(fakeResponse);
sandbox
.stub(accountAssignment, 'assignCreditAccount')
.resolves(fakeResponse);
sandbox
.stub(accountAssignment, 'assignDebitAccount')
.resolves(fakeResponse);
sandbox
.stub(accountAssignment, 'assignFullAccount')
.resolves(fakeResponse);
sandbox
.stub(accountAssignmentControls, 'accountAssignmentHeader')
.resolves(fakeResponse);
sandbox
.stub(t, 'expect')
.resolves(fakeResponse);
sandbox
.stub(t, 'contains')
.resolves(fakeResponse);
sandbox
.stub(t, 'takeScreenshot')
.resolves(fakeResponse);
await accountAssignment.assignAccounts({});
expect(startAccountAssignment).to.be.callledOnce;
});
});
});
});
});
运行时得到
TypeError:无法在 Context._callee$ (C:/Projects/Platform/PlatformTesting/UITests-NotProtractor/ unit-tests/business-functions/account-assignment.tests.js:49:14) at tryCatch (node_modules\regenerator-runtime\runtime.js:65:40) at Generator.invoke [as _invoke] (node_modules\regenerator- runtime\runtime.js:303:22) 在 Generator.prototype.(anonymous function) [as next] (node_modules\regenerator-runtime\runtime.js:117:21) 在步骤 (unit-tests\business-functions\account -assignment.tests.js:21:191) 在 C:\Projects\Platform\PlatformTesting\UITests-NotProtractor\unit-tests\business-functions\account-assignment.tests.js:21:437 at new Promise () at语境。(unit-tests\business-functions\account-assignment.tests.js:21:99)