【问题标题】:Jasmine expected spy function to have been called on backbone.viewJasmine 预计在主干视图上调用了间谍功能
【发布时间】:2016-09-09 15:32:30
【问题描述】:

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

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

【问题讨论】:

    标签: javascript jasmine jasmine-jquery gulp-jasmine


    【解决方案1】:

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

    and.callThrough()
    

    尝试将您的示例修改为:

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

    看看这是否能帮助你解决问题:)

    【讨论】:

    • 我已经尝试过了,但错误仍然是“预期的间谍接近已被调用。” - 还有其他建议吗?
    猜你喜欢
    • 2012-09-26
    • 2023-03-06
    • 2019-02-05
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多