sinon 存根错误 "attempted to wrap undefined property of job as function"
sinon stub error "attempted to wrap undefined property of job as function"
我正在尝试使用 sinon 存根来测试我的函数,该函数包含两个名为 job 和 job1 的变量。如何给他们临时值以避免函数值。
在其中一个文件 myFunction.js 中,我有类似
的函数
function testFunction() {
var job = this.win.get.value1 //test
var job1 = this.win.get.value2 // test1
if(job === 'test' && job1 === 'test1') {
return true;
}
return false;
}
我正在尝试使用 karma 测试 testFunction,我尝试用我的值存根两个值,以便它可以覆盖函数值
it('should test my function', function(done) {
var stub = sinon.stub('job','job1').values('test','test1');
myFunction.testFunction('test', function(err, decodedPayload) {
decodedPayload.should.equal(true);
done();
});
});
我遇到错误 "attemted to wrap undefined property of job as function"
首先,您可以将 testFunction 简化为以下内容。
function testFunction() {
return this.win.get.value1 === 'test' && this.win.get.value2 === 'test1';
}
这里没有任何异步操作,因此在您的测试中您不需要使用 done()。
Sinon 的 'stub' 文档建议您应该使用 sandbox 功能来存根非函数属性。
从你的问题中不清楚 'this' 的上下文是什么,所以我假设你的测试已经实例化了你正在使用名称 'myFunction' 测试的任何东西(你的测试暗示).
还不清楚 'win' 和 'get' 是什么,所以这里假设它们是对象。
不要忘记 restore() 沙箱,以免污染后续测试。
it('should test my function', function() {
var sandbox = sinon.sandbox.create();
sandbox.stub(myFunction, 'win').value({
get: {
value1: 'test',
value2: 'test1',
}
});
myFunction.testFunction().should.equal(true);
sandbox.restore();
});
我正在尝试使用 sinon 存根来测试我的函数,该函数包含两个名为 job 和 job1 的变量。如何给他们临时值以避免函数值。
在其中一个文件 myFunction.js 中,我有类似
的函数function testFunction() {
var job = this.win.get.value1 //test
var job1 = this.win.get.value2 // test1
if(job === 'test' && job1 === 'test1') {
return true;
}
return false;
}
我正在尝试使用 karma 测试 testFunction,我尝试用我的值存根两个值,以便它可以覆盖函数值
it('should test my function', function(done) {
var stub = sinon.stub('job','job1').values('test','test1');
myFunction.testFunction('test', function(err, decodedPayload) {
decodedPayload.should.equal(true);
done();
});
});
我遇到错误 "attemted to wrap undefined property of job as function"
首先,您可以将 testFunction 简化为以下内容。
function testFunction() {
return this.win.get.value1 === 'test' && this.win.get.value2 === 'test1';
}
这里没有任何异步操作,因此在您的测试中您不需要使用 done()。
Sinon 的 'stub' 文档建议您应该使用 sandbox 功能来存根非函数属性。
从你的问题中不清楚 'this' 的上下文是什么,所以我假设你的测试已经实例化了你正在使用名称 'myFunction' 测试的任何东西(你的测试暗示).
还不清楚 'win' 和 'get' 是什么,所以这里假设它们是对象。
不要忘记 restore() 沙箱,以免污染后续测试。
it('should test my function', function() {
var sandbox = sinon.sandbox.create();
sandbox.stub(myFunction, 'win').value({
get: {
value1: 'test',
value2: 'test1',
}
});
myFunction.testFunction().should.equal(true);
sandbox.restore();
});