Node.js unittest Stubbing Own function



如果之前有人问过这个问题,我们深表歉意。这是我想在文件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');
});
});
sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});

resolveThing必须是对象,'resolveThing'必须是对象中的函数,如果属性还不是函数,则会引发异常。

我认为这就是你的情况。

最新更新