2

我刚刚开始使用 Jasmine 并尝试第一次设置一些测试。我有一个骨干系列。我想我会将我的集合作为 beforeEach() 方法的一部分,然后针对它执行测试。

我有一个测试 json 对象,我在为我的应用程序制作原型时使用了它,因此我宁愿重用该对象进行测试,而不是模拟调用。

到目前为止,这是我的代码(它失败了)。

describe("Vehicle collection", function() {
    beforeEach(function() {
        this.vehicleCollection = new Incentives.VehiclesCollection();
        this.vehicleCollection.url = '../../json/20121029.json';
        this.vehicleCollection.fetch();
        console.log(this.vehicleCollection);
    });

    it("should contain models", function() {
        expect(this.vehicleCollection.length).toEqual(36);
        console.log(this.vehicleCollection.length); // returns 0
    });
});

当我在 beforeEach 方法中 console.log 时——控制台看起来像这样......

d {length: 0, models: Array[0], _byId: Object, _byCid: Object, url: "../../json/20121029.json"}

奇怪的是,当我在 Chrome 开发人员工具中展开对象(小三角形)时——我的集合完全填充了一系列车辆模型等。但我的测试仍然失败:

Error: Expected 0 to equal 36

我想知道是否需要利用“waitsFor()”方法?

更新(带有工作代码)

谢谢您的帮助!

@ deven98602——你让我走上了正确的道路。最终,这个“waitsFor()”实现终于奏效了。我希望这段代码对其他人有帮助!如果这是一种糟糕的技术,请发表评论。谢谢!

describe("A Vehicle collection", function() {

    it("should contain models", function() {

        var result; 
        var vehicleCollection = new Incentives.VehiclesCollection();
        vehicleCollection.url = '/json/20121029.json';
        getCollection();

        waitsFor(function() {
            return result === true;
        }, "to retrive all vehicles from json", 3000);

        runs(function() {
            expect(vehicleCollection.length).toEqual(36);
        });

        function getCollection() {
            vehicleCollection.fetch({
                success : function(){
                    result = true;
                },
                error : function () {
                    result = false;
                }
            });
        }

    });
});
4

2 回答 2

2

Just glancing at your code, it looks to me like fetch has not yet populated the collection when you run the expectation.

You can use the return value from fetch to defer the expectation until the response is received using waitsFor and runs:

beforeEach(function() {
    this.vehicleCollection = new Incentives.VehiclesCollection();
    this.vehicleCollection.url = '../../json/20121029.json';
    var deferred = this.vehicleCollection.fetch();
    waitsFor(function() { return deferred.done() && true });
});

it("should contain models", function() {
    runs(function() {
        expect(this.vehicleCollection.length).toEqual(36);
    });
});

I haven't actually tried this can't guarantee that it will work as-is, but the solution will look something like this. See this article for more on asynchronous testing with Jasmine.

于 2012-11-16T02:30:56.480 回答
1

collection.fetch() 是异步调用,接受成功和错误回调

var result; 
this.collection.fetch({success : function(){
result = true;
}})

waitsFor(function() {
return response !== undefined;
}, 'must be set to true', 1000);


runs(function() {
expect(this.vehicleCollection.length).toEqual(36);
console.log(this.vehicleCollection.length); // returns 0
});
于 2012-11-16T02:41:06.227 回答