Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon stub throwing Attempted to wrap object which is already wrapped

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.

like image 403
david419 Avatar asked Dec 14 '16 06:12

david419


People also ask

How do you clear a Sinon stub?

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() .

What does Sinon restore do?

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.

What is Sinon Spy ()?

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.

What is Sinon sandbox?

JS. Sandboxes removes the need to keep track of every fake created, which greatly simplifies cleanup. var sandbox = require("sinon").


2 Answers

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();
  });
like image 69
Gordon Avatar answered Oct 03 '22 00:10

Gordon


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/

like image 35
Tonino Avatar answered Oct 03 '22 02:10

Tonino