2

I want to test the status of te response from one url, saving in an object, but karma shows me the following error: Expected Promise ({ &&state: Object ({ status:0})}) to be (whatelse)

Mi service is:

angular.module('ServiceModule', []).
  service('anonymous_Login_Services', ['$http','$q', function($http,$q){

 var data={dato:null,estado:null};

this.loadData=function(){
    var defered=$q.defer();
     $http({
          method: 'POST',
          url: 'http://example.com/',
          headers: {

            'Content-Type': 'application/x-www-form-urlencoded'
           },
          data: "grant_type=client_credentials"
          })  
    .success(function(inf,state){
        data.dato=inf;
        data.estado=state;
        defered.resolve(data);               
    })
    .error(function(inf,state){
        data.dato=inf;
        data.estado=state;
        defered.reject(data);  
    });
    return defered.promise;
}

 }]);

And the jasmine test:

describe('Test servicios', function () {

var anomlogin, $httpBackend, $q;

beforeEach(module('ServiceModule', function($provide) {

}));

beforeEach(inject(function (_$httpBackend_,_$q_, _anonymous_Login_Services_) {
  $httpBackend = _$httpBackend_;
  anomlogin = _anonymous_Login_Services_;
  $q = _$q_;
}));

it('Token', function () {

   var status2=0;

     var estado= anomlogin.loadData(); 
    console.log(estado);

        expect(anomlogin.loadData()).toBe(status2);

});

});

I dont wanna use a mock to the http post with httpbackend, i want to compare to objects, one of them obtained by the post in the service.

Thanks a lot.

1 Answers1

0

Your test is meaningless if you don't check the resolved value of the promise. That's what jasmine's done parameter is for:

function failTest(error) {
    expect(error).toBeUndefined();
}

it('Token', function (done) {
    var status2=0;

    anomlogin.loadData()
        .then(function (result) {
            expect(result).toBe(status2);
        })
        .catch(failTest)
        .finally(done);
});
JLRishe
  • 99,490
  • 19
  • 131
  • 169