mardi 1 décembre 2015

test a promise with angularjs

I have this factory and her test:

angular.module('testApp')
  .factory('bar', function ($q) {
    // Service logic
    // ...

    var meaningOfLife = 42;

    // Public API here
    return {
      someMethod: function () {
        var deferred = $q.defer();

        setTimeout(function() {
          deferred.resolve();
        }, 1000);

        return deferred.promise;
      }
    };
  });


'use strict';

describe('Service: bar', function() {

  // load the service's module
  beforeEach(module('testApp'));

  it('should do something', function(done) {
    inject(function (bar) {
        var promise = bar.someMethod();
        promise.then(function(){
          expect(!!bar).toBe(true);
          done();
        });
      });
  });
});

The test returns this:

Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

I know replacing setTimeout with $timeout in the factory and adding $timeout.flush() in the test, it works.

However, I would like to understand how i can work this test with setTimeout instead of $timeout.

Aucun commentaire:

Enregistrer un commentaire