I am trying to use sinon stub to test my function that contains two variables called job and job1. How to give temporary values to them to avoid function values.
In one of the file myFunction.js I have functions like
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;
}
and I am trying to test testFunction using karma and I tried to stub two values with my values so it can override the function values
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();
});
});
I am getting error "attemted to wrap undefined property of job as function"
First of all you could simplify your testFunction to the following.
function testFunction() {
return this.win.get.value1 === 'test' && this.win.get.value2 === 'test1';
}
There's nothing asynchronous going on here so in your test you don't need to use done().
Sinon's 'stub' documentation suggests you should be using the sandbox feature to stub non-function properties.
It's not clear from your question what your context of 'this' is so I'll assume your tests have instantiated whatever it is you're testing with the name 'myFunction' (which your test implies).
It's also unclear what 'win' and 'get' are so this will assume they are objects.
Don't forget to restore() the sandbox so you don't pollute subsequent tests.
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();
});