Node.js - Mocha done() 方法在以前的测试中导致错误
Node.js - Mocha done() method causing errors in previous tests
我正在用 Chai 为我在 Node.js 中编写的应用程序实现 Mocha 作为我的测试框架。此规范是为secureId.js
编写的。
// secureId.js
"use strict"
const bcrypt = require('bcrypt');
// Constructor for SecureID
function SecureID(str, rounds, func) {
// Makes salt and hash unable to be changed or viewed outside the member functions
let hashedID;
let gennedSalt;
bcrypt.genSalt(rounds, (err, salt) => {
gennedSalt = salt;
bcrypt.hash(str, salt, (err, hash) => {
hashedID = hash;
func(err, salt, hash);
});
});
// Gets the salt associated with the instance
this.getSalt = function() {
return gennedSalt;
};
// Gets the hash associated with the instance
this.getHash = function() {
return hashedID;
};
// Set new id for already instantiated SecureID
this.setNewId = function(str, rounds, func) {
bcrypt.genSalt(rounds, function(err, salt) {
gennedSalt = salt;
if (err)
func(err);
bcrypt.hash(str, salt, function(err, hash) {
hashedID = hash;
func(err, salt, hash);
});
});
};
// set new id for already instantiated SecureID synchronously
this.setNewIdSync = function(str, rounds) {
gennedSalt = bcrypt.genSaltSync(rounds);
hashedID = bcrypt.hashSync(str, gennedSalt);
};
// Compares a string and hash
this.equals = function(str, func) {
bcrypt.compare(str, hashedID, function(err, res) {
func(err, res);
});
};
// Compares a string and hash synchronously
this.equalsSync = function(str) {
return bcrypt.compareSync(str, hashedID);
};
};
exports.SecureID = SecureID;
这是规格。
"use strict";
let expect = require('chai').expect;
describe('SecureID', () => {
let ids = [];
let anID = 'aLongIDofCharacters';
let anAmountOfRounds = 10;
let SecureID = require('../secureId').SecureID;
console.log(`Tests with rounds >= 20 will take over an hour to complete. You are doing ${anAmountOfRounds} round(s).`);
describe('#SecureID()', () => {
let i = 0;
let checkingFunction = (done) => {
if (i == 2) {
clearInterval(checkingFunction);
done();
};
};
it('Create an ID', (done) => {
for (let j = 0; j <= 1; j++) {
ids.push(new SecureID(anID, anAmountOfRounds, (err, salt, hash) => {
expect(err).to.be.undefined;
expect(salt).not.to.be.undefined;
expect(hash).not.to.be.undefined;
i++;
}));
};
setInterval(checkingFunction, 100, done);
});
});
describe('#getHash()', () => {
it('Returns a hash.', () => {
expect(ids[0].getHash()).not.to.be.undefined;
expect(ids[1].getHash()).not.to.be.undefined;
});
it('Returns a hash unique to that generated from the same ID.', () => {
expect(ids[0].getHash()).not.to.equal(ids[1].getHash());
});
});
describe('#getSalt()', () => {
it('Returns a salt.', () => {
expect(ids[0].getSalt()).not.to.be.undefined;
expect(ids[1].getSalt()).not.to.be.undefined;
});
it('Returns a salt unique to that generated from the same ID.', () => {
expect(ids[1].getSalt()).not.to.equal(ids[0].getSalt());
});
});
describe('#setNewId()', () => {
let i = 0;
let checkingFunction = (done) => {
if (i == 2) {
clearInterval(checkingFunction);
done();
};
};
it('Sets a new ID asynchronously.', (done) => {
for (let j = 0; j <= 1; j++) {
ids[j].setNewId(anID, (err, salt, hash) => {
let previousHash = ids[j].getHash();
let previousSalt = ids[j].getSalt();
expect(err).to.be.undefined;
expect(salt).not.to.equal(previousSalt);
expect(hash).not.to.equal(previousHash);
i++;
});
};
setInterval(checkingFunction, 100, done);
});
});
describe('#setNewIdSync()', () => {
it('Sets a new ID synchronously.', () => {
for (let j = 0; j <= 1; j++) {
let previousHash = ids[j].getHash();
let previousSalt = ids[j].getSalt();
ids[j].setNewIdSync(anID);
expect(ids[j].getSalt()).not.to.equal(previousSalt);
expect(ids[j].getHash()).not.to.equal(previousHash);
};
});
});
describe('#equals()', () => {
it('Compares an ID with a hash, calling a callback with the result of genHash(ID) == hash.', () => {
it('Hash is not equal to an empty string.', (done) => {
ids[0].equals('', (err, res) => {
expect(res).to.equal(false);
expect(err).to.be.undefined;
done();
});
});
it('Hash is equal to original ID.', (done) => {
ids[0].equals(anID, (err, res) => {
expect(res).to.equal(true);
expect(err).to.be.undefined;
done();
});
});
});
});
describe('#equalsSync()', () => {
it('Compares an ID with a hash, returning the result of genHash(ID) == hash (synchronous).', () => {
it('Hash is not equal to an empty string.', () => {
expect(ids[0].equalsSync('')).to.equal(false);
});
it('Hash is equal to original ID.', () => {
expect(ids[0].equalsSync(anID)).to.equal(true);
});
});
});
});
我的问题是,当我到达#setNewId()
时,我得到了以下测试失败的原因:done() called multiple times
。我明白这个错误是什么意思,但我不明白的是,当 Mocha 输出测试结果时,当它到达#setNewId()
时,它会显示
1) Create an ID
✓ Sets a new ID asynchronously. (107 ms)
同样,#setNewIdSync()
会产生多次调用 done 错误,但似乎试图验证#setNewId()
;它在 Mocha 中的结果是
✓ Sets a new ID synchronously. (252 ms)
2) Sets a new ID asynchronously.
有什么帮助吗?我只是在做傻事吗?
所以,事实证明我在做一些愚蠢的事情。这只是错误地清除间隔的问题。
前面的代码...
describe('#SecureID()', () => {
let i = 0;
let checkingFunction = (done) => {
if (i == 2) {
clearInterval(checkingFunction);
done();
};
};
it('Create an ID', (done) => {
for (let j = 0; j <= 1; j++) {
ids.push(new SecureID(anID, anAmountOfRounds, (err, salt, hash) => {
expect(err).to.be.undefined;
expect(salt).not.to.be.undefined;
expect(hash).not.to.be.undefined;
i++;
}));
};
setInterval(checkingFunction, 100, done);
});
});
尝试清除间隔checkingFunction
不存在时。调用setInterval(checkingFunction, ...)
将使用checkingFunction
方法设置间隔,但是,名称checkingFunction
不存在所述间隔。因此,修复实际上很简单:
describe('#setNewId()', () => {
it('Sets a new ID asynchronously.', (done) => {
let i = 0;
let checkingInterval = setInterval( () => {
if (i == 2) {
clearInterval(checkingInterval);
done();
};
}, 100);
for (let j = 0; j <= 1; j++) {
ids.push(new SecureID(anID, anAmountOfRounds, (err, salt, hash) => {
expect(err).to.be.undefined;
expect(salt).not.to.be.undefined;
expect(hash).not.to.be.undefined;
i++;
}));
};
});
});
该行let checkingInterval = setInterval( () => {
创建一个名为 checkingInterval
的新间隔,稍后在异步测试完成后清除该间隔。
相关文章:
- QUnit 测试错误与 IE(Internet Explorer)
- Angular 2组件测试错误:无法解析'TestComponentBuilder'
- 带有expect的mocha不适用于测试错误
- 茉莉花 - 如何测试错误
- 使用摩卡在节点上测试错误.js函数
- 余烬集成测试错误.处理异步副作用
- 用javascript测试错误的reCAPTCHA站点密钥
- 摩卡茶意外断言测试错误从对象等于对象
- 黑客等级测试错误.count
- 无法运行内部功能测试.错误:[POST http://localhost:4444/wd/hub/session] co
- 烬你好字-测试错误在http://localhost:4200/tests,但不是在命令行
- Angular JS官方教程-第2步单元测试错误
- 单元测试错误输出"致命错误:在结束"之后写入
- 使用equal和deeequal进行单元测试错误
- Javascript对数组元素的测试错误
- Ionic App的Karma单元测试错误:[ng:areq] Argument '不是函数,没有定义
- Angular 2 - http测试-错误:"Cannot read property 'getCoo
- Mocha单元测试错误处理库
- Node.js如何使用mocha获得更好的异步测试错误消息
- 量角器屏幕截图测试错误代码105