我正在尝试测试 Backbone 集合的获取。我正在使用 Sinon 的假服务器来设置假 REST 端点。问题是请求似乎没有发送。
我将 Jasmine 与 Karma 结合使用,并通过 PhantomJS 运行它。
问题是请求显然没有被发送。没有任何错误,但控制台没有记录任何内容。
代码如下:
describe("The Posts collection", function() {
var posts;
var server;
beforeEach(function() {
server = sinon.fakeServer.create();
posts = new PostCollection();
});
afterEach(function() {
server.restore();
});
it("should fetch the posts from the api", function() {
server.respondWith("GET", "/posts",
[200, { "Content-Type": "application/json" },
'{ "stuff": "is", "awesome": "in here" }']);
posts.fetch({
success: function(model, response, options) {
console.log("REQUEST SENT");
}
});
});
});
最佳答案
事实证明,我没有足够仔细地阅读文档。对于假服务器,您需要告诉它响应。我在调用 posts.fetch()
后添加了以下内容:
server.respond();
现在一切正常。
关于backbone.js - BackboneJS 测试集合获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28839564/