Sinon error Attempted to wrap function which is already wrapped

前端 未结 9 582
滥情空心
滥情空心 2020-12-24 04:01

Though there is a same question here but I could not find answer to my problem so here goes my question:

I am testing my node js app using mocha and chai. I am usin

相关标签:
9条回答
  • 2020-12-24 04:59

    This error is due to not restoring the stub function properly. Use sandbox and then create the stub using the sandbox. After each test inside the suite, restore the sandbox

      beforeEach(() => {
          sandbox = sinon.createSandbox();
          mockObj = sandbox.stub(testApp, 'getObj', fake_function)
      });
    
      afterEach(() => {
          sandbox.restore();
      });
    
    0 讨论(0)
  • 2020-12-24 05:04

    For anyone running into this issue, if you stub or spy on the entire object, and you later do

    sandbox.restore()

    You'll still get the error. You have to stub/spy the individual methods.

    I wasted forever trying to figure out what was wrong.

    sinon-7.5.0

    0 讨论(0)
  • 2020-12-24 05:04
    function stub(obj, method) {
         // try to remove any existing stub in case it exists
          try {
            obj[method].restore();
          } catch (e) {
            // eat it.
          }
          return sinon.stub(obj, method);
        }
    

    and use this function when creating stubs in tests. It will resolve 'Sinon error Attempted to wrap function which is already wrapped' error.

    example:

    stub(Validator.prototype, 'canGeneratePayment').returns(Promise.resolve({ indent: dTruckIndent }));
    
    0 讨论(0)
提交回复
热议问题