【发布时间】:2025-12-07 17:00:02
【问题描述】:
如果之前有人问过这个问题,我们深表歉意。这是我想在文件getStuff.js 中进行单元测试的模块。我很难存根这里使用的resolveThing 模块。
getStuff.js
const resolveThing = require('./resolveThing.js');
module.exports = async function getStuff(target, stuff) {
const { element, test, other } = resolveThing(target);
try {
return element;
} catch (error) {
throw new Error('Did not work.');
}
};
这是我在单元测试中使用 sinon 进行存根的尝试。但是,当我尝试运行它时,它会出现 TypeError: Cannot stub non-existent own property resolveType 错误。有谁知道我怎样才能让这个测试工作?
const getStuff = require('../com/getStuff');
const resolveThing = require('../com/resolveThing');
const mochaccino = require('mochaccino');
const { expect } = mochaccino;
const sinon = require('sinon');
describe('com.resolveThing', function() {
beforeEach(function () {
sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
});
afterEach(function () {
resolveThing.restore();
});
it('Standard message', function() {
const answer = getAttribute('a','b');
expect(answer).toEqual('a');
});
});
【问题讨论】:
标签: javascript node.js unit-testing tdd sinon