这是我用来连接两个字符串的基本JS,context.getVariable
是我要使用Sinon模拟的东西,
//util.js
var p;
function concat(p) {
var first_name = context.getVariable('first_name');
var res = p.concat(first_name);
return res;
}
concat(p);
我已添加此test.js
,
var expect = require('expect.js');
var sinon = require('sinon');
var rewire = require('rewire');
var app = rewire('./util.js');
var fakeContext = {
getVariable: function(s) {}
}
var contextGetVariableMethod;
beforeEach(function () {
contextGetVariableMethod = sinon.stub(fakeContext, 'getVariable');
});
afterEach(function() {
contextGetVariableMethod.restore();
});
describe('feature: concat', function() {
it('should concat two strings', function() {
contextGetVariableMethod.withArgs('first_name').returns("SS");
app.__set__('context', fakeContext);
var concat = app.__get__('concat');
expect(concat("988")).to.equal("988SS");
});
});
我在跑步,
node_modules.bin>摩卡R:\ abc \ js-unit-test \ test.js
util.js:7
var first_name = context.getVariable('first_name');
^
TypeError: context.getVariable is not a function
答案 0 :(得分:0)
您需要在此处导入实际上下文,然后使用sinon.stub模拟该getVariable方法,以便在您的实际代码运行时它将获取该方法。
navigation.xml