[removed] Mocking Constructor using Sinon

前端 未结 8 2123
迷失自我
迷失自我 2021-02-06 20:47

I am pulling my hair out trying to figure out how to mock a constructor using sinon. I have a function that will create multiple widgets by calling a constructor that accepts a

8条回答
  •  日久生厌
    2021-02-06 21:44

    I used Mockery to Mock a Constructor/Function without any problems.

    var mockery = require('mockery');
    var sinon = require('sinon');
    
    mockery.enable({
      useCleanCache: true,
      warnOnReplace: false,
      warnOnUnregistered: false
    });
    
    exports.Client = function() {/* Client constructor Mock */};
    var ClientSpy = sinon.spy(exports, 'Client');
    mockery.registerMock('Client', ClientSpy);
    
    var Factory = require('Factory'); // this module requires the Client module
    

    You should be able to apply a Sinon Spy just as the example above does.

    Make sure to disable or reset Mockery after the test(s)!

提交回复
热议问题