Although there are posts regarding this , I could not fix it using before, after functions and restoring the objects. Posting the code below:-
var Log = sinon.stub(hello, 'someEvent', function(type, name){
var obj = {};
obj.addData = function(data){
return;
};
obj.complete = function(){
return;
}
return obj;
}),
someVar = sinon.stub(servicecore, 'some'),
The error I get is:-
Attempted to wrap someEvent which is already wrapped.
And
Attempted to wrap some which is already wrapped.
Can someone help with this?
Edited below
I even tried with before and after functions as suggested:-
var Log,someVar;
before(function(){
Log = sinon.stub(hello, 'someEvent', function(type, name){
var obj = {};
obj.addData = function(data){
return;
};
obj.complete = function(){
return;
}
return obj;
});
someVar = sinon.stub(servicecore, 'some');
});
after(function(){
Log.restore();
someVar.restore();
});
Tried even with beforeEach and afterEach functions but same error.
One important function to remember is sinon. reset() , which resets both the behavior and history of all stubs. If you just want to reset a specific stub you can use stub. reset() .
For sandboxes, it resets the history of all stubs created in the sandbox. resetBehavior : only available for stubs and sandboxes, it resets the stub's original behavior. reset : for stubs, resets both behavior and history; for sandboxes, resets the internal states of everything created in the sandbox.
Sinon spies are used to record information about function calls. Unlike mocks or stubs, spies do not replace the function being called. Spies just record what parameters the function was called with, what value it returned, and other information about the function execution.
JS. Sandboxes removes the need to keep track of every fake created, which greatly simplifies cleanup. var sandbox = require("sinon").
From the sinon documentation:
var stub = sinon.stub(object, "method"); Replaces object.method with a stub function. The original function can be restored by calling object.method.restore(); (or stub.restore();). An exception is thrown if the property is not already a function, to help avoid typos when stubbing methods.
The stub is normally restored after the test is complete using the after or afterEach hooks.
after(function() {
// runs before all tests in this block
someVar.restore();
});
afterEach(function() {
// runs before each test in this block
someVar.restore();
});
you are stubbing someVar.some not someVar itself so you need to restore its method:
someVar.some.restore();
If you still have problems, then try to stub using the following method.
someVar.some = sinon.stub();
Looks like the same but it is not :) (discovered after hours of swearing)
Anyway let's try the sandbox in sinon to stub from it and just restore the sandbox at the end
https://sinonjs.org/releases/v1.17.7/sandbox/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With