4

I'm getting " timeout of 2000ms exceeded. Ensure the done() callback is being called in this test." while unit testing a service call that responds back with a promise. I am expecting a rejected promise.

UNIT TEST - Karma-Mocha-Chai running on 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.");
        }
      );


    });

 });


});

Below is the service i am testing. I tested the teamService while running the site and it does successfully return a rejected promise.

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

I've read through other stack overflow answers and non was successful.

Any ideas?

I appreciate the help.

Thanks,

Alexander
  • 105
  • 7
  • When I'm stumped by a test failure, I strip it back to basics. Just put in a passing `it`. Then add the `before`, then the `beforeEach`. That'll ensure your set-up isn't the thing causing an issue. – Adrian Lynch Sep 19 '15 at 15:52
  • It doesn't look like your test is async in nature. You don't have a `done` argument. Doesn't that make mocha run the test without waiting on any async calls? – Adrian Lynch Sep 19 '15 at 15:54
  • @AdrianLynch thanks for the reply. A friend took a look and said I was missing $rootScope.$apply. I will need to look more into it myself on why this was needed. – Alexander Sep 19 '15 at 16:03
  • Possible duplicate of [In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded.](http://stackoverflow.com/questions/16607039/in-mocha-testing-while-calling-asynchronous-function-how-to-avoid-the-timeout-er) – Ryan Wu Jan 27 '17 at 15:43

1 Answers1

2

A friend took a look into it and immediately saw the issue. Apparently I needed do a 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();

});

});

Alexander
  • 105
  • 7