Jasmine Testing with Angular Promise not resolving with TypeScript

I have a fairly straightforward test that works against an Angular promise, which I'm resolving in the beforeEach function, but the then in my code is not ever firing and I can't see what I'm missing. These are written with TypeScript, but that doesn't really have any bearing on the problem.

Here is my test

describe('Refresh->', () => {

  var controller = new Directives.Reporting.ReportDirectiveController($scope, $q, $location);
  var called = false;
  var defer: any;

  beforeEach((done) => {
    controller.drillReport = (drillReport: Models.drillReport): ng.IPromise<Models.drillData> => {
      defer = $q.defer();
      called = true;
      return defer.promise;
    spyOn(controller, 'processResults');

  it('Calls DrillReport', () => {

  it('Calls ProcessResults', () => {

The Refresh method in the controller looks like this:

refresh() {
  this.drillReport({ drillReport: drillReport })
    .then((results: Models.drillData) => {
      parent.processResults(results, parent.availableDrills, this.columns, this.gridOptions, undefined, undefined);



What you are missing is that you will need access to use $scope, or $rootScope, so that you can call and force a digest cycle...


The reason this is needed is that the resolved and rejected promises are processed during the digest loop. So while you are resolving the promise in your mock, the actual promise callback is not being invoked.


Following up on what @Brocco said, your code is calling done() before the promise is processed.

What you need it a way for your test code to know that that parent.processResults() has been called.

I suggest you have refresh return a promise that will be resolved just after parent.processResults(), and add controller.refresh().finally(() => { done(); }); in your test code.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.