1

私はダミーを持っていますBackbone.Model

App.Models.Note = Backbone.Model.extend({
      default: {
          title: ''
      }
);

そして、Backbone.View次のような私のモデルの場合:

  App.Views.NoteView = Backbone.View.extend({

    template: ...,

    initialize: function () {
        this.listenTo(this.model, "change", this.render);
        this.render();
    },

     render: function () {
        this.$el.html(this.template({
            title: this.model.get("title")
        }));
        return this;
     }
  });

テストには、mocha.js + chai + sinon を使用します。次のテストがあります。

 describe("App.Views.NoteView", function () {
      beforeEach(function () {
         this.view = new App.Views.NoteView({
              el: this.$fixture,
              model: new App.Models.Note()
        });
      }

      afterEach(function () {
          this.view.model.destroy();
      });

      it("my try 1", function () {
           var mySpy1 = sinon.spy(this.view, "render");

           this.view.model.set({
                 title: "a new Title"
           });

           expect(this.view.render).to.have.been.calledOnce;
       });
 }

テストしようとしているのは、renderメソッドをスパイすることです。モデル属性を変更すると、renderメソッドが呼び出されます。ただし、レンダリングが正常に実行されても、テストでエラーが発生します

'expected render to be called once but was called 0 times'

何か助けはありますか?

4

1 に答える 1