【问题标题】:BackboneJS testing collection fetchBackboneJS 测试集合获取
【发布时间】:2015-05-04 13:29:44
【问题描述】:

我正在尝试测试 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");
      }
    });
  });
});

【问题讨论】:

    标签: backbone.js jasmine karma-runner karma-jasmine sinon


    【解决方案1】:

    事实证明,我没有仔细阅读文档。使用假服务器,您需要告诉它做出响应。我在调用posts.fetch()后添加了以下内容:

    server.respond();
    

    现在完美运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      相关资源
      最近更新 更多