简体   繁体   English

使用Backbone和Jasmine测试将自定义事件绑定到回调函数

[英]Test binding of a custom event to a callback function with Backbone and Jasmine

I'm trying to test to make sure a custom event was bound to upon showing of a dialog. 我正在尝试测试以确保在显示对话框时绑定了自定义事件。 Here is my code: 这是我的代码:

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

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

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

Note: setupListener is a private function. 注意:setupListener是一个私有函数。 Here's my test code that works: 这是我有效的测试代码:

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

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

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

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

    });

But now I want to check the "on" function was called with correct parameters: 但是现在我要检查是否使用正确的参数调用了“ 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);

    });

But I get the error: 但是我得到了错误:

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

The problem looks to be with the handler. 问题似乎出在处理程序上。 How do I check the "on" function was called with the handler? 如何检查处理程序调用的“ on”功能?

您应该像这样包装处理程序函数:

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

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

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