Testing AngularJS.then () return value using jasmine

This is my AngularJS:

self.fetch = {
 things: function() {
   return $http.get("/things/")
     .then(function(response) {
         return response.data;
   });
 }
};

And this is my test:

it('fetch.things() should GET to /things/, success', function() {
    mockBackend.expectGET("/thingss/").respond({stuffs: 'stuffs'});

    var response = BaseService.fetch.things();

    mockBackend.flush();

    expect(response).toEqual({stuffs: 'stuffs'})
});

And this is the error I get:

Expected Object({ $$state: Object({ status: 1, value: Object({ stuffs: 'stuffs' }) }) }) to equal Object({ stuffs: 'stuffs' }).
        at Object.<anonymous> (tests/test_base.js:28:26)
Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.102 seChromium 53.0.2785 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.119 secs / 0.102 secs)

What is this additional status: 1,material and where is it from?

+4
source share
1 answer

Try the following:

it('fetch.things() should GET to /things/, success', function() {
   mockBackend.expectGET("/things/").respond({stuffs: 'stuffs'});
   var p = BaseService.fetch.things();

   mockBackend.flush();

   p.then(function(response){
      expect(response.data).toEqual({stuffs: 'stuffs'})
   });

   $rootScope.digest();

   return p;
});  
+1
source

Source: https://habr.com/ru/post/1673448/


All Articles