【问题标题】:Testing BackboneJs Sinon测试 BackboneJs Sinon
【发布时间】:2017-06-20 17:07:03
【问题描述】:

我正在尝试在我的 Backbone 类中检查我的方法。这种方法有效,但我需要用 mochaJS 和 SinonJS 编写单元测试

describe("Foodtype View", function () {

    before(function () {
        this.$fixture = $("<div id='note-view-fixture'></div>");
    });

    beforeEach(function () {
        // Empty out and rebind the fixture for each run.
        this.$fixture.empty().appendTo($("#fixtures"));
        this.spy = sinon.spy();

        var a=[{"id":1,"name":"Pizza","lastModificationDate":"2017-02-03T09:01:58.754Z"}];
        this.view = new app.FoodtypeView({
            el: this.$fixture,
            collection: new app.FoodtypeCollection(a)
        });
    });

    after(function () {
        // Remove all subfixtures after test suite finishes.
        $("#fixtures").empty();
    });

    it("Adding models", function () {
        this.view.addNewFoodtype(this.spy);
    });

查看方法addNewFoodtype

        addNewFoodtype: function () {

            var newFoodtype = $('#newFoodtype').val();

            if (newFoodtype == "") {
                console.log("Wpisz nazwę Typu jedzenia !!");
            } else {
                var newFoodtypeModel = new app.Foodtype({"name": _.escape(newFoodtype)});
                this.collection.add(newFoodtypeModel);
            }
        }
  1. 如何检查我的方法在集合中添加模型?

  2. 在我看来如何传递参数var newFoodtype = $('#newFoodtype').val();

【问题讨论】:

    标签: backbone.js sinon


    【解决方案1】:
    1. 检查集合 findWhere 方法返回模型与 name 等于您的模拟;
    2. 将 ID 为 newFoodtype&lt;select&gt; 添加到您的模拟中,该选项已选中。

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2019-07-27
      • 2014-07-31
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多