0

Gist:

  1. I spy on get method of my rest service:

    spyOn(restService, 'get').and.callFake(function () {
          return deferred.promise;
    });  
    
  2. The method I am trying to test is myService.getFormData() that returns a chained promise:

    function getFormData() {
          var getPromise = this.restService.get(endPoint, params, true);
          var processedDataPromise = then(successHandle, failHandler);
          return processedDataPromise;
    }  
    
  3. Back to Jasmine spec, I invoke getFormData function and make assertions:

    var processedDataPromise = myService.getFormData();
    processedDataPromise.then(function(data) {
       expect(data).not.toBeNull();  
    });
    deferred.resolve(testFormData);
    $rootScope.$digest();
    

The Problem:
The above derivative promise (processedDataPromise) does indeed get resolved. However the 'data' passed to it is undefined. Is it anything to do with $digest cycles not doing its job in Jasmine?

Why does Jasmine not pass any data to the above derived promise.?
Further Note: The processedDataPromise is a completely new promise than the get returned promise.

It is a promise for processedData which as we can see is returned by successHandle (Definition not shown) once its parent getPromise gets resolved.

In UI everything works like a Charm.

rave
  • 1,022
  • 1
  • 12
  • 23
Ace
  • 1,501
  • 4
  • 30
  • 49
  • Did you mean `getPromise.then(…)`? And are you sure that `successHandle` always returns the expected result? Also, did you try catching errors? – Bergi Dec 16 '16 at 15:47

1 Answers1

0

Sorry for posting the question. The derivative promise indeed got the resolved data I was referring to. The problem was that I was incorrectly accessing the JSON data inside of successHandle.

As a result the 'successHandle' returned null and the the processedDataPromise got back undefined response.

Stupid mistake, very hard to find, but the best part is the learning and understanding of JS Promises.

Ace
  • 1,501
  • 4
  • 30
  • 49