关于AJAX调用的Sinon超时

时间:2015-08-22 00:40:41

标签: javascript ajax sinon

使用Mocha Chai和Sinon,我有一个测试,从揭示模块模式中取回特定记录。测试失败,超时。我应该如何测试从AJAX请求中分配变量的方法?

Test.js

 <select name="YEAR">
            <option value="">Pilih tahun ajaran</option>
            <?php
            include 'connect.php';
            $sql="SELECT DISTINCT SUBSTRING(idSoal,1,4) AS tahun FROM hasiljwb ;";
            $hasil= mysql_query($sql) or die(mysql_error());
            while($row=mysql_fetch_array($hasil)){

            echo '<option value="' . $row['tahun'] . '">' . $row['tahun']  . '</option>';
            }
            ?>
 </select>

Employee.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();
      });
    });
  });
})();

1 个答案:

答案 0 :(得分:0)

您需要告诉FakeServer何时回复。请参阅FakeServer API文档以供参考。

例如:

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
});