gpt4 book ai didi

javascript - 使用 Backbone 和 Jasmine 测试自定义事件与回调函数的绑定(bind)

转载 作者:行者123 更新时间:2023-11-28 08:16:44 25 4
gpt4 key购买 nike

我正在尝试测试以确保在显示对话框时绑定(bind)自定义事件。这是我的代码:

    setupListener = function () {
appEvent.on('some_event', theHandler);
};

theHandler = function (responseData) {
....
};

this.show = function () {
setupListener();
};

注意:setupListener 是一个私有(private)函数。这是我的有效测试代码:

    it('appEvent.on was called', function () {

spyOn(appEvent, 'on');
dialogView.show();

var theHandler = function (responseData) {
....
};

expect(appEvent.on).toHaveBeenCalled('some_event', theHandler);

});

但现在我想检查“on”函数是否使用正确的参数调用:

    it('appEvent.on was called with right parameters', function () {

spyOn(appEvent, 'on');
dialogView.show();

var theHandler = function (responseData) {
....
};

expect(appEvent.on).toHaveBeenCalledWith('some_event', theHandler);

});

但我收到错误:

Expected spy on to have been called with [ 'some_event', Function ] but actual calls were [ 'some_event', Function ]

问题似乎出在处理程序上。如何检查处理程序是否调用了“on”函数?

最佳答案

您应该将处理程序函数包装如下:

expect(appEvent.on).toHaveBeenCalledWith('some_event', jasmine.any(theHandler));

关于javascript - 使用 Backbone 和 Jasmine 测试自定义事件与回调函数的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491070/

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