繁体   English   中英

尝试模拟对象实例时 Sinon 存根不起作用

[英]Sinon stub not working when trying to mock out an instance of an object

我正在尝试模拟使用对象的new关键字创建的每个实例。

这是我要模拟的对象:

var SharedWhiteboardView = function(moduleEl, domService) {
'use strict';

var self;
var sharedWhiteboardProblemImage;
var whiteboardController;
var caller = false;
var toolbarController;

return {
    initWhiteboard : function()
    {
        self = this;
        sharedWhiteboardProblemImage = domService.find(moduleEl, '#sharedWhiteboardModule-sharedWhiteboardProblemImage');

        var toolbarEL = $('#sharedWhiteboard-toolbar');
        toolbarController = new ToolbarController(WhiteboardConstants.SHARED_WHITEBOARD_ID, toolbarEL, null);
        toolbarController.init(false);
        whiteboardController = toolbarController.getWhiteboardController();
    },

    enableWhiteboardEdition : function(enabled)
    {
        if(self.getWhiteboardObject() && self.getWhiteboardObject.hasOwnProperty('enableEdition')) self.getWhiteboardObject().enableEdition(enabled);
        whiteboardController.setEnabled(enabled);
    }
  };
}

这是我要测试的文件,它创建了上述对象的一个新实例

Box.Application.addModule('SharedWhiteboardModule', function(context) {
'use strict';

var self;
var moduleEl;
var domService;
var sharedWhiteboardView;
var modal;
var assignmentTimer = 3000;
var sharing = false;
var assignmentImageData = '';

return {
    /**
     * Initializes the module and caches the module element
     * @returns {void}
     */
    init: function() {
        self = this;
        domService = context.getService('DomService');
        moduleEl = context.getElement();
        sharedWhiteboardView = new SharedWhiteboardView(moduleEl, domService);
        sharedWhiteboardView.initWhiteboard();
        sharedWhiteboardView.enableWhiteboardEdition(false);
  };
}

我正在尝试编写一个单元测试来测试是否使用“false”调用了 sharedWhiteboardView.enableWhiteboardEdition 方法

但是,我没有附加间谍或将该方法存根。 我已经尝试过这些解决方案,但它们没有用

//第一次尝试

  sinon.stub(SharedWhiteboardView, "enableWhiteboardEdition", function() {return 0})

// 第二次尝试

 sinon.stub(SharedWhiteboardView.prototype, "enableWhiteboardEdition").returns(0);

//第三次尝试

sandbox.stub(SharedWhiteboardView.prototype, 'enableWhiteboardEdition', checkEnableWhiteboardEdition());

//第四次尝试尝试chrmod提供的答案

it.only('when type is "SharedWhiteboardModule-setEditable" should call sharedWhiteboardView.enableWhiteboardEdition', function (done) {
  const view = SharedWhiteboardView();
  sinon.stub(view, "enableWhiteboardEdition", function() {
    console.log('Hit');
  });
  module.onmessage('SharedWhiteboardModule-setEditable', true);
  done();
});

没有错误,但它没有命中 console.log,我按照建议删除了“new”关键字

我得到的错误:

-试图将未定义的属性 enableWhiteboardEdition 包装为函数 -无法存根不存在的自有属性 enableWhiteboardEdition

请任何帮助都会很棒。 我在这里走到了死胡同。

这是一个代码笔: http ://codepen.io/anon/pen/bgmNxx?editors=0011

我想要做的就是让 Fake 方法在我的模块调用 enableEdition 时被命中

SharedWhiteboardView不是构造函数,而是工厂函数。 一旦被调用(没有 new),它返回具有enableWhiteboardEdition作为自己属性的新对象。

因此,必须在该对象上设置存根: const view = SharedWhiteboardView(); sinon.stub(view, "enableWhiteboardEdition", function() {return 0}); const view = SharedWhiteboardView(); sinon.stub(view, "enableWhiteboardEdition", function() {return 0});

这做到了。

it('when type is "SharedWhiteboardModule-setEditable" should call setEditable with appropriate callback', function (done) {
    var mockSharedWhiteboardView = {
        enableWhiteboardEdition: function() {},
        initWhiteboard: function() {},
        initScrollBar: function() {},
        refreshScrollBar: function() {},
        isMainWhiteboardAvailable: function() {}
    };
    sandbox.spy(mockSharedWhiteboardView, 'enableWhiteboardEdition');

    var tempGlobals = {
        SharedWhiteboardView: global.SharedWhiteboardView
    };

    global.SharedWhiteboardView = function() {
        return mockSharedWhiteboardView;
    };

    module = Box.Application.getModuleForTest('SharedWhiteboardModule', contextFake);
    module.init();

    var shouldEnable = true;
    module.onmessage('SharedWhiteboardModule-setEditable', shouldEnable);
    assert(mockSharedWhiteboardView.enableWhiteboardEdition.calledWithExactly(shouldEnable),
           'should enable the whiteboard');

    shouldEnable = false;
    module.onmessage('SharedWhiteboardModule-setEditable', shouldEnable);
    assert(mockSharedWhiteboardView.enableWhiteboardEdition.calledWithExactly(shouldEnable),
           'should not enable the whiteboard');

    // cleanup
    global.SharedWhiteboardView = tempGlobals.SharedWhiteboardView;

    done();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM