Это мой базовый 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> mocha R: \ abc \ js-unit-test \ test.js
util.js:7
var first_name = context.getVariable('first_name');
^
TypeError: context.getVariable is not a function