Backbone测试返回超时

时间:2015-02-12 16:23:38

标签: unit-testing backbone.js mocha

在为我的Backbone应用程序运行以下测试时,我收到timeout of 2000ms exceeded消息。

如何通过此测试? 我也在试着在调用more时听取事件被触发。怎么能测试呢?

describe("Foo.Collection.Items/Discover", function () {
    describe("More method", function () {

        beforeEach(function () {

            this.server = sinon.fakeServer.create();

            this.hasLength = sinon.spy(Foo.View.ItemFeed.prototype, "toggleFeedback");
            this.noLength = sinon.spy(Foo.View.ItemFeed.prototype, "stopLazyload");

            this.item1 = new Foo.Model.item({
                description: "A swell minions movie!",
                for_sale: false,
                title: "Minions: Goldfinger",
                link: "/discover",
                'private': false,
                'main_image': false
            });

            this.item2 = new Foo.Model.item({
                description: "A round pot",
                for_sale: true,
                title: "Pot",
                link: "/discover",
                'private': true,
                'main_image': true
            });

            this.itemsDiscover = new Foo.Collection.Items([this.item1, this.item2], {url: "/discover"});

        });

        afterEach(function () {

            this.server.restore();
            this.hasLength.restore();
            this.noLength.restore();

        });

        it("should fetch items and trigger moreFetched", function (done) {


            this.server.respondWith('GET', "/discover", [
                200,
                {"Content-type": "application/json"},
                JSON.stringify([this.item1, this.item2])
            ]);

            this.itemsDiscover.once("add", function () {
                expect(this.itemsDiscover).to.have.length(2);
                expect(this.hasLength).to.be.calledOnce();

                done();
            });

            this.itemsDiscover.more();

        });
    });
});

我试图测试的主干部分Collection

more: function () {
    var collection = this;
    this.fetch({
        success: function (collection, response) {
            if (response.length) {
                collection.trigger('moreFetched');
            } else {
                collection.trigger('emptyFetched');
            }
        },
        reset: false,
        remove: false
    });
}

2 个答案:

答案 0 :(得分:1)

我猜您有一个例外,无法拨打done,请尝试

this.itemsDiscover.once("add", function () {
  try {
    expect(this.itemsDiscover).to.have.length(2);
    expect(this.hasLength).to.be.calledOnce();
    done();
  } catch(e) {
    done(e);
  }
});

如果您收到错误,请发布,因为我认为它是什么。

答案 1 :(得分:0)

我想我知道发生了什么:您的测试方法取决于被触发的add事件,以便进入验证数据的回调。但是,您设置this.itemsDiscover的方式已经包含this.item1this.item2

....
this.itemsDiscover = new Foo.Collection.Items([this.item1, this.item2], {url: "/discover"});

然后,您对URL的模拟也会返回包含this.item1this.item2的数组的字符串版本:

...
this.server.respondWith('GET', "/discover", [
            200,
            {"Content-type": "application/json"},
            JSON.stringify([this.item1, this.item2])
        ]);
...

这里是踢球者:集合永远不会触发add事件,因为集合中返回的已经存在两个项目。 d'哦

我建议您更改测试以等待sync事件,这会在成功fetch之后始终触发:

this.itemsDiscover.once("sync", function () {
    expect(this.itemsDiscover).to.have.length(2);
    expect(this.hasLength).to.be.calledOnce();

    done();
});

我希望这会让你的测试代码运行。