摩卡,超过2000ms的超时时间.确保在这个测试中调用了done()回调

Mocha & Chai- "timeout of 2000ms exceeded. Ensure the done() callback is being called in this test."

本文关键字:调用 测试 done 回调 2000ms 超过 超时 确保 时间 摩卡      更新时间:2023-09-26

我收到"超时时间超过2000ms "。确保在此测试中调用了done()回调",同时对使用承诺响应的服务调用进行单元测试。我在等一个被拒绝的承诺。

单元测试 - Karma-Mocha-Chai在PhantomJS上运行

describe('teamService', function () {
  var teamSrvc, q;
  beforeEach(angular.mock.module('scCommon'));
  beforeEach(angular.mock.inject(function ($q, teamService) {
    teamSrvc = teamService;
    q = $q;
  }));
  describe('getTeamsByNameStartWith', function () {
    it('should return reject promise when passing invalid text to search', function () {
      var invalidFirstArg = 132;
      var validSecondArg = 10;
      return teamSrvc.getTeamsByNameStartWith(invalidFirstArg, validSecondArg).then(
        function (result) {
        },
        function (err) {
          err.should.equal("Invalid text to search argument passed.");
        }
      );

    });
 });

});
下面是我正在测试的服务。我在运行站点时测试了teamService,它确实成功返回了一个被拒绝的承诺。
(function (ng) {
'use strict';
ng.module('scCommon')
.service('teamService', ['$q', '$http', function ($q, $http) {

  var getTeamsByNameStartWith = function (textToSearch, optLimit) {
    var defer = $q.defer();
    if (typeof textToSearch != "string") {
      defer.reject("Invalid text to search argument passed.");
      return defer.promise;
    } else if (typeof optLimit != "number") {
      defer.reject("Invalid limit option argument passed.");
      return defer.promise;
    }

    $http.get('url')
      .success(function (data) {
        defer.resolve(data);
      })
      .error(function () {
        defer.reject("There was an error retrieving the teams");
      });
    return defer.promise;
  };

  return {
     getTeamsByNameStartWith: getTeamsByNameStartWith
  }
}])
})(angular);

我已经阅读了其他堆栈溢出的答案,并且non是成功的。

任何想法?

谢谢你的帮助。

谢谢,

一个朋友看了一下,立刻发现了这个问题。显然我需要做一个rootScope.$apply.

describe('teamService', function () {

 var teamSrvc, q, rootScope;
  beforeEach(angular.mock.module('scCommon'));
  beforeEach(angular.mock.inject(function ($q, teamService, $rootScope) {
    teamSrvc = teamService;
    q = $q;
    rootScope = $rootScope;
  }));
  describe('getTeamsByNameStartWith', function () {
it('should return reject promise when passing invalid text to search', function () {
  var invalidFirstArg = 132;
  var validSecondArg = 10;
  teamSrvc.getTeamsByNameStartWith(invalidFirstArg, validSecondArg).then(
    function (result) {
    },
    function (err) {
      err.should.equal("Invalid text to search argument passed.");
    }
  );
  rootScope.$apply();
});
it('should return reject promise when passing invalid number to limit', function () {
  var validFirstArg = "alex";
  var invalidSecondArg = "10";
  teamSrvc.getTeamsByNameStartWith(validFirstArg, invalidSecondArg).then(
    function (result) {
    },
    function (err) {
      err.should.equal("Invalid limit option argument passed.");
    }
  );
  rootScope.$apply();
});

});