csdn产品小助手 2015-08-22 00:40 采纳率: 0%
浏览 9

Sinon在AJAX呼叫上超时

Using Mocha Chai and Sinon, I have a test to get back a specific record from a revealing module pattern. The test fails with a timeout. How should I test a method to assign variables from a AJAX request?

Test.js

(function () {
  'use strict';

  describe('Employee Module', function() {
    var server,
        employeeJSON = {
          "employeeTemplate" : [
            {
              "userId": 1
            }
          ]
        };

    before(function () {
      server = sinon.fakeServer.create();
      server.respondWith(
        "GET",
        "/employees.json",
        [200, { "Content-Type": "application/json" }, JSON.stringify(employeeJSON)]
      );
    });

    after(function () {
      server.restore();
    });

    it('should get Employee by ID', function(done) {

      var employee = new Employee(),
          employeeData;

      employee.getData(1).done( function (data) {
        employeeData = data.employeeTemplate[0];
        assert.equal(employeeData.userId, 1, 'Employee ID equals 1');
        done();
      });
    });
  });
})();

Employee.js

var Employee = function() {

  var EmployeeInfo = {};

  var loadUserinfo = function(userid) {
    return $.ajax({
      type: 'GET',
      data:{userid: userid},
      url: '/employees.json',
      dataType: 'json',
      async: true,
      success: function(data) {
        return data;
      }
    });
  };

  var getData = function (userid) {
    return loadUserinfo(userid).done();
  };

  return {
    getData: getData
  };

};
  • 写回答

1条回答 默认 最新

  • ??yy 2015-08-31 07:05
    关注

    You need to tell FakeServer when to respond. See the FakeServer API docs for reference.

    For example:

    it('should get Employee by ID', function(done) {
    
      var employee = new Employee(),
          employeeData;
    
      employee.getData(1).done( function (data) {
        employeeData = data.employeeTemplate[0];
        assert.equal(employeeData.userId, 1, 'Employee ID equals 1');
        done();
      });
    
      server.respond(); // Please respond mr. sinon so my test may continue
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器