Jasmine期望在backbone.view上调用间谍功能

时间:2016-09-09 15:32:30

标签: javascript jasmine jasmine-jquery gulp-jasmine

我无法通过茉莉花测试来了解间谍。

当我运行以下测试时,我可以在控制台中看到输出CLOSE EVENT,但测试triggers close失败。

如何使用间谍正确编写测试?

define([
    'backbone'
], function(Backbone){
    describe('TEST', function(){
        beforeEach(function(){
            this.view = new (Backbone.View.extend({
                initialize: function(){
                    _(this).bindAll('close');
                    this.$el.append($('<span>', {class: 'closeview'}));
                    $('body').append(this.$el);
                    this.$el.on('click', '.closeview', this.close);
                },
                close: function(){
                    console.log('CLOSE EVENT');
                }
            }));
        });
        it('exists', function(){
            expect(this.view.$el).toBeVisible();
        });
        it('triggers close', function(){
            spyOn(this.view, 'close');
            this.view.$el.find('.closeview').trigger('click');
            expect(this.view.close).toHaveBeenCalled();
        });
    });
});

1 个答案:

答案 0 :(得分:1)

当您监视某个函数时,实际上是在对该方法进行存根。如果您只是想检查函数是否被调用但是执行内容很重要,则需要添加:

and.callThrough()

尝试将您的示例修改为:

spyOn(this.view, 'close').and.callThrough();

看看这是否可以帮助您解决问题:)