小编Ale*_*can的帖子

Backbone.js在浏览器中使用Sinon Spies查看测试

我正在为Backbone View编写一个测试,以测试在获取模型后是否正在调用render函数.测试是:

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();
        }
    });   
});
Run Code Online (Sandbox Code Playgroud)

我正在使用Sinon Spies将间谍对象附加到profileView视图对象的render函数.

该观点是:

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"); …
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js backbone.js sinon

9
推荐指数
1
解决办法
2797
查看次数

标签 统计

backbone.js ×1

javascript ×1

mocha.js ×1

sinon ×1