gpt4 book ai didi

javascript - 尝试模拟对象实例时,Sinon stub 不起作用

转载 作者:行者123 更新时间:2023-11-28 05:01:13 24 4
gpt4 key购买 nike

我试图模拟使用对象的 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方法

但是我无法附加该方法的 spy 或 stub 。我已经尝试过这些解决方案,但它们不起作用

//第一次尝试

  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包装为函数-无法 stub 不存在的自有属性enableWhiteboardEdition

请提供任何帮助。我已经走到了死胡同。

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

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

最佳答案

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

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

关于javascript - 尝试模拟对象实例时,Sinon stub 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42123257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com