2

我正在尝试使用 PhantomJS 浏览器为带有 Karma 和 Jasmine 的 AngularJS 服务编写单元测试。我要测试的服务使用嵌套的 Promise(用于读取 IndexedDB 数据库、从服务器获取响应并将响应写回数据库)。

写入数据库时​​也必须这样做(写入本地数据库,发布/放入服务器,使用服务器 ID 更新本地数据库)。除了处理与服务器的连接丢失的测试之外,我正在编写的测试都可以工作。

发生这种情况时,sync() 方法将在线状态设置为 false 并创建一个 $timeout,通过递归调用重复检查连接。如果连接再次建立,所有未同步的数据将被同步(并且相应的 sync() 调用的承诺将被解析)。

describe('Harmonized', function() {
var ... // setting all 'global' variables

[...] 

beforeEach(function(){
  // Address that the service sends a ping to check if online again
  pingGet = backend.when('GET', 'http://localhost:2403');
  pingGet.respond(0);

  testTablePut121 = backend.when('PUT', 'http://localhost:2403/test_table/121');
  testTablePut121.respond(function(method, url, data, headers) { ... } // returns 200

  [...]
}

[...]

it('should try to sync a SAVED element but should fail because of missing server connectivity', function() {
  var list;
  var timeoutCounter = 0;
  _checkOnline = harmonized._checkOnline; // harmonized is the service that has to be testet
  harmonized._checkOnline = function () {
    // only call the original function two times
    if (timeoutCounter++ < 2) {
      _checkOnline();
      // flush the $timeout and the $httpBackend
      timeout.flush(2000);
      backend.flush(1);
      // this also gives me an error because of the $digest already in progress
      console.log('after tick');
    }
  };


  all.getList().then(function(entries) {
    list = entries;

    testTablePut121.respond(0);
    pingGet = backend.expect('GET', 'http://localhost:2403');
    pingGet.respond(0);

    spyOn(list[0], 'sync').and.callThrough();
    list[0].save().then(
    // resolve is called when the server call was successfull
    function() {
      expect(list[0]._synchronized).toBeTruthy();
      expect(list[0].sync.calls.count()).toBe(2);
      console.log('done');
    }, 
    // reject is called when the server response an error other than 0 (which is not the case in this test spec)
    null, 
    // notify is called when the local db save is made
    function(){
      expect(list[0].sync.calls.count()).toBe(1);
      expect(list[0]._synchronized).toBeFalsy();
    });
    backend.flush(1);
  });
  backend.flush(1);
});

当第二次调用 backend.flush() 函数时(刷新在 save() 函数中发出的 http 请求,我收到一条错误消息,告诉我 $digest 已经在进行中。省略该行,不刷新请求(并且因此也会出错)。

有没有办法在正确的时间刷新所有内容而不会出现 $digest 错误?

4

0 回答 0