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

时间:2014-05-06 09:40:09

标签: javascript backbone.js jasmine

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

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

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

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

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

    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"函数是用处理程序调用的吗?

1 个答案:

答案 0 :(得分:0)

您应该将处理函数包装为:

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