简体   繁体   中英

Unit test using jasmine for Service

Below is the convention followed in our project. Services will call resource files and returns promise.

This is my Service

angular.module('myModule').factory('myService', function(myResource) {
  return {
    exportToExcel: function(params) {
      return myResource.exportToExcel($.param(params)).$promise;
    },
    getUsers: function(term) {
      return myResource.getUsers({ term: term }).$promise;
    }
  }
});

And this is my Resource file

angular.module('myModule').factory('myResource', function($resource) {
  return $resource('/report/', {}, {
    exportToExcel: {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      responseType: 'arraybuffer',
      url: '/abc/qwer',
      transformResponse: function(data, headers) {
        var response = {};
        response.data = data;
        response.headers = headers;
        return response;
      }
    },
    getUsers: {
      method: 'GET',
      url: '/abc/xyz',
      isArray: true
    }
  })
});

I am finding it difficult to write unit test cases for these using jasmine. Could some one help with this as i am new to unit test cases.

I searched in google but could not find the examples suiting my need

This is typical example of how to test angular factories. You can have a look at the followings :

http://embed.plnkr.co/2i7IHs/

Testing an AngularJS factory with Karma (Jasmine)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM