【问题标题】:Backbone.js view tests using Sinon Spies in a browserBackbone.js 在浏览器中使用 Sinon Spies 查看测试
【发布时间】:2012-03-26 07:25:36
【问题描述】:

我正在为 Backbone 视图编写一个测试,以测试在获取模型后是否调用了渲染函数。测试是:

beforeEach(function () {
    $('body').append('<div class="sidebar"></div>');
    profileView = new ProfileView();
});

it('should call the render function after the model has been fetched', function (done) {
    profileView.model = new UserModel({md5: 'd7263f0d14d66c349016c5eabd4d2b8c'});
    var spy = sinon.spy(profileView, 'render');
    profileView.model.fetch({
        success: function () {
            sinon.assert.called(spy);
            done();
        }
    });   
});

我正在使用 Sinon Spies 将 spy 对象附加到 profileView 视图对象的渲染函数。

观点是:

var ProfileView = Backbone.View.extend({
    el: '.sidebar'
  , template: Hogan.compile(ProfileTemplate)
  , model: new UserModel()
  , initialize: function () {
        this.model.bind('change', this.render, this);
        this.model.fetch();
    }
  , render: function () {
        $(this.el).html(this.template.render());
        console.log("Profile Rendered");
        return this;
    }
});

在测试中调用 fetch 后,更改事件被触发并且视图的渲染函数被调用,但是 Sinon Spy 没有检测到渲染被调用并且失败。

作为一个实验,我尝试在测试中调用渲染函数来查看 Spy 是否识别它:

it('should call the render function after the model has been fetched', function (done) {
    profileView.model = new UserModel({md5: 'd7263f0d14d66c349016c5eabd4d2b8c'});
    var spy = sinon.spy(profileView, 'render');
    profileView.render();
    profileView.model.fetch({
        success: function () {
            sinon.assert.called(spy);
            done();
        }
    });   
});

间谍检测到调用是在上述情况下进行的。

有人知道为什么 Spy 在我的初始测试中没有识别渲染调用吗?

【问题讨论】:

    标签: javascript backbone.js mocha.js sinon


    【解决方案1】:

    只需 3 次猜测:

    1. 您假设fetch({ success }) 回调在Model 已更新并触发模型事件后被调用.. 可能不是这样。 解决方案:尝试在here 周围进行调试
    2. 也许fetch 调用不是success,所以没有调用成功回调。 解决方案:尝试向fetch 添加一个错误回调,看看是否有我们被发送到的地方。
    3. Model.validate 响应 false,如果您还没有实现它,不太可能

    (我赌2.

    【讨论】:

      【解决方案2】:

      这对我有用,尽管在集合中使用了间谍:

      var thatzzz = this;
          this.hcns.fetch({
              success: function(){
                  expect( thatzzz.hcnsFetchSpy ).toHaveBeenCalled();
              }
      });
      
      • 在 beforeEach 上定义了间谍

      【讨论】:

        【解决方案3】:

        问题是在视图的构建过程中,初始化函数将一个事件直接绑定到渲染函数。您无法使用间谍拦截此事件,因为在您设置间谍之前绑定已经发生(您在构建之后执行)。

        解决方案是在构建视图之前监视原型。因此,您必须将间谍移动到 beforeEach 中,或者将视图的构造移动到测试中。

        设置间谍:

        this.renderSpy = sinon.spy(ProfileView.prototype, 'render');
        this.view = new ProfileView();
        

        然后,删除间谍:

        ProfileView.prototype.render.restore()
        

        这将监视“普通”渲染调用,以及来自模型的更改事件。

        【讨论】:

        • 如果视图是单例的,那么视图文件将导出新的 ProfileView() 我将如何访问原型,因为它没有原型,而是具有 proto 和绑定窥探那也不起作用?
        猜你喜欢
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多