How to write unit test to Restangular with Jasmine?


I know how to write test to GET methods, but what with other methods? PUT, PATCH, DELETE?

This is for example my service method to remove user:

removeOne: function(user) {
      var deferred;
      deferred = $q.defer();
      if (_.isUndefined( || _.isNaN( {
      } else {
        user.remove().then(function(result) {
          AlertsServ.logSuccess('Użytkownik został usunięty');
          return deferred.resolve(result);
        }, function(err) {
          return deferred.reject(err);
      return deferred.promise;

It should be something like this:

describe('DELETE /users/1', function() {
  beforeEach(function() {
    return $httpBackend.expect('DELETE', BACKEND_URL + '/users/1').respond(200, 'user1');
  return describe('removeOne method', function() {
    return it('should get one user', function() {
      var user;
      user = {
        id: 1,
        routes: 'users'
      return UserServ.removeOne(user);

but how to define user?

P.S.: How to define in Jasmine/Karma Restangular base url?


Now, with more experience in unit testing, I think that I should mock Restangular and test only if Reastangular are properly called with arguments. I shouldn’t add $httpBackend in this tests.

In unit tests, I should check if my services working good, not external libraries. Am I right?

Answered By – dakolech

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.