TypeError: parsed is undefined on angularjs service unit test

angularjs, angularjs-service, jasmine, javascript, unit-testing

Solution

The `parsed` variable is the URL from the service in question. It is `undefined` for one of the following reasons:

- URL is malformed

- $http.get is not called

- `token` is not defined

- `sucess` and `error` callbacks have no `data`

- `.respond` is not called

- `.respond` does not include a response object as an argument

For example:

 describe('simple test', test);

 function test()
   {
   it('should call inviteService and pass mock data', foo);

   function foo() 
     {
     module('myapp.services');
     inject(myServiceTest);

     function myServiceTest(inviteService, $httpBackend)
       {
       $httpBackend.expect('GET', /.*/).respond(200, 'bar');

       function callback(){};

       inviteService.getInvite.token = '1123581321';
       inviteService.getInvite(callback, callback);

       $httpBackend.flush();

       expect(callback).toHaveBeenCalledOnce();
       }
     }
   }

References

AngularJS source: angular-mocksSpec.js - "should throw exception when only parsed body differs from expected body object"

AngularJS source: urlUtils.js

AngularJS source: urlUtilsSpec.js

Problem

I am trying to make a unit test for a service that uses $http. I am using Jasmine and I keep on getting this error: TypeError: parsed is undefined in angular.js (line 13737) This is what my service looks like: ``` angular.module('myapp.services', []) .factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) { var inviteService = { token: '', getInvite: function(callback, errorCallback) { $http.get('/invites/' + this.token + '/get-invite') .success(function(data) { callback(data); }) .error(function(data, status, headers, config) { errorCallback(status); }); } }; return inviteService; }]); ``` This is what my test looks like: ``` describe ('Invite Service', function () { var $httpBackend, inviteService, authRequestHandler; var token = '1123581321'; beforeEach(module('myapp.services')); beforeEach(inject(function ($injector) { $httpBackend = $injector.get('$httpBackend'); authRequestHandler = $httpBackend.when('/invites/' + token + '/get-invite').respond({userId: 'userX'}, {'A-Token': 'xxx'}); inviteService = $injector.get('inviteService'); })); afterEach (function () { $httpBackend.verifyNoOutstandingExpectation (); $httpBackend.verifyNoOutstandingRequest (); }); describe ('getInvite', function () { beforeEach(function () { inviteService.token = token; }); it ('should return the invite', function () { $httpBackend.expectGET('/invites/' + token + '/get-invite'); inviteService.getInvite(); $httpBackend.flush(); }); }); }); ``` I am pretty new to unit testing angularjs based apps and I used the example in the angularjs documentation https://docs.angularjs.org/api/ngMock/service/$httpBackend I am not sure what I could be missing, and I already tried different things and I always get the same error, any help will be appreciated.

Original source