很抱歉,如果以前有人问过这个问题。这是我要在文件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');
});
});
答案 0 :(得分:0)
sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
resolveThing
必须是对象,而'resolveThing'
必须是对象中的函数,如果该属性还不是函数,则抛出异常。
我认为这就是您的情况。