how to test and resolve Controller data (.then function()) promise and get orginal Data in Jasmine2
Asked Answered
O

2

0
    I am testing a controller that uses a service that returns a promise. I need to resolve promise. I am using Jasmine 2. 

    Here is Spec code

      beforeEach(inject(function ($controller, $rootScope, _myService_, _$q_, _$rootScope_, _$httpBackend_, $http) {


         scope = $rootScope.$new();
         $q = _$q_;
         $httpBackend = _$httpBackend_;
         $rootScope = _$rootScope_;
         myService = _myService_;
$http = $http;
         ctrl =  $controller('Ctrl', { '$scope': scope, 'myService': myService });
     spyOn(myService, "getDateRangeData").and.callThrough();

      }));


        it('getDateRangeData return Data obj', function() {

    myService.getDateRangeData().then(function(response) {
      console.log('Success', response);
    }); 
        scope.$digest()      

      });

service js
function getDateRangeData(obj) {
  return $http({
    method: 'POST',
    url: 'https:URL',
     headers: {
       'Content-Type': 'application/json',
       'X-Auth-Token': self.token
     },
    data: obj
  })
}

console not returning any obj.Shows error.Unexpected request: POST https:URL No more request expected. i need data from Ctrl . In Crtl I am getting data but not in testcase. deferred. how to get Api data. Api data is object. or there is another aprroch to get Ctrl return promise to resolve and getData? added sevice js code where request send.

can anyone help soon please.

Orozco answered 5/12, 2018 at 9:34 Comment(0)
U
0

If you want the spyOn to actually use the correct implementation instead of the mock you can use callThrough() instead of callFake().

Try it like this:

spyOn(myService, "getDateRangeData").and.callThrough();
Usually answered 6/12, 2018 at 8:29 Comment(4)
Yes tried that and shows error Unexpected request: POST "URL Link" is shown No more request expected. Without Spy can I get Ctrl data?Orozco
@Orozco the callThrough will actually perform the operations that getDateRangeData() do. Make sure you have specified in your beforeEach what the dependencies on myService should do when they are called. For example, if you inject and use $http in that service, you need to define what it should return when a specific url is called to it.Alizaalizarin
tried and updated code. still error is same. and placed var to check then function it returns Promise{$$state: Object{status: 0}} it is not resolving my promiseOrozco
@Höglund: Tried with sample API still I am getting same error Unexpected request: GET jsonplaceholder.typicode.com/todos/1 No more request expected error properties: Object({ $$passToExceptionHandler: true }) can please let me know where i am making mistake?Orozco
M
0

First of all you are "spying on" the wrong method. We use spyOn for two reasons:

  • To expect(method).toHaveBeenCalled
  • To mock the return value

In your case the spyOn does not achieve any of these two.

You should spyOn the $http instead. Since the actual http call is not required for your test, the reason being: the objective is not to test $http.

this.$http = $http;
spyOn(this, '$http').and.callFake(function(args) {
    return {
        then: function(fn) {
            return fn('response');
        }
    };
});

And in it block:

it('getDateRangeData return Data obj', function() {
    myService.getDateRangeData('test')
    .then(function(response) {
        console.log('Success', response);
        expect(response).toEqual('response');
    });
    expect(this.$http).toHaveBeenCalledOnceWith('test');   
});
Mummer answered 16/12, 2018 at 4:5 Comment(1)
@ Sree.Bh Thanks for reply.I have tried this but promise is not resolved nothing print in console and getting an error expect(...).toHaveBeenCalledOnceWith is not a functionOrozco

© 2022 - 2024 — McMap. All rights reserved.