繁体   English   中英

茉莉花预期的间谍功能已在ribs.view上调用

[英]Jasmine expected spy function to have been called on backbone.view

我的茉莉花测试无法理解间谍。

当我运行以下测试时,我可以在控制台中看到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();
        });
    });
});

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

and.callThrough()

尝试将您的示例修改为:

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

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

暂无
暂无

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

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