gpt4 book ai didi

javascript - Node.js - Mocha did() 方法在之前的测试中导致错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:49 26 4
gpt4 key购买 nike

我正在使用 Chai 来实现 Mocha 作为我在 Node.js 中编写的应用程序的测试框架。此规范是为 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()被调用多次。我明白这个错误的意思,但我不明白的是,当Mocha输出测试结果时,当它到达#setNewId()时,它显示

1) Create an ID
✓ Sets a new ID asynchronously. (107 ms)

同样,#setNewIdSync() 会产生多次调用完成错误,但似乎是尝试验证#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 的新间隔,稍后当异步测试完成时会在其内部清除。

关于javascript - Node.js - Mocha did() 方法在之前的测试中导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38228949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com