lundi 29 février 2016

$httpBackend is not working with angular-oauth in Karma+Jasmine+AngularJS environment

I have injected "angular-oauth2" in my module. "OAuth" is an provider of "angular-oauth2" module. I have written a new service "OauthSupport" in my module and injected the "OAuth" in to my service. I am calling functions(getAccessToken,getRefreshToken,revokeToken) of "OAuth" from my service.

Its working fine. Only problem is with unit-testing.

While calling the function "getAccessToken" a post call sent to the URL "http://localhost:3000/oauth/token" from "angular-oauth2" and data received from server.

My unittest call the function from my service myService.AccToken(props), this function calls the "getAccessToken" method from "OAuth" and makes post call. So I wrote mock for $http like as below

beforeEach(inject(function($injector) {
      $httpBackend = $injector.get('$httpBackend');
      $httpBackend.when('POST', 'http://localhost:3000/oauth/token').respond({
       errorCode: 'OK'
      }); 
});

But its not working, I am not getting response and done() function not get called. Finally getting error like as

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

I have some other services which makes post and get calls, I wrote unit testing and testcases are got succeed with the same $hhtp($httpBackend) mock object. Only for this service am getting error. Please someone help me to fix this Issue. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire