2

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;
      defer.resolve({});
      return defer.promise;
    };
    spyOn(controller, 'processResults');
    controller.refresh();
    done();
  });

  it('Calls DrillReport', () => {
    expect(called).toBeTruthy();
  });

  it('Calls ProcessResults', () => {
    expect(controller.processResults).toHaveBeenCalled();
  });
});

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

2 Answers 2

3

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...

$scope.$digest();

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I knew that, but had forgotten about it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.