Angular Karma Jasmine forEach@ Error

I'm trying to test an interceptor with Karma and Jasmine, but having unexpected errors. I was following this code: http://jbavari.github.io/blog/2014/06/20/testing-interceptor-headers-in-angularjs/

Code:

angular
    .module('xos.helpers',[
      'ngCookies',
      'xos.xos',
      'xos.hpcapi',
      'xos.xoslib',
      'bugSnag'
    ])
    .config(config);

function config($httpProvider, $resourceProvider) {
  console.log('xos.helpers config')
  $httpProvider.interceptors.push('SetCSRFToken');

  $interpolateProvider.startSymbol('{$');
  $interpolateProvider.endSymbol('$}');

  // NOTE http://www.masnun.com/2013/09/18/django-rest-framework-angularjs-resource-trailing-slash-problem.html
  $resourceProvider.defaults.stripTrailingSlashes = false;
}

Test:

  'use strict';
    describe('The xos.helper module', function(){
      var SetCSRFToken, httpProviderObj;

      beforeEach(module('xos.helpers'));

      beforeEach(module(function(_$httpProvider_){
        httpProviderObj = _$httpProvider_;
      }));

      beforeEach(inject(function(_SetCSRFToken_){
        SetCSRFToken = _SetCSRFToken_;
      }));

      it('should exist', () => {
        expect(SetCSRFToken).toBeDefined();
      });

      it('should set SetCSRFToken interceptor', () => {
        expect(httpProviderObj).toBeDefined();
        expect(httpProviderObj.interceptors).toContain('SetCSRFToken');
      });
});

In karma conf all needed files are loaded but I'm getting this unknown error:

/Users/teone/Sites/xos/views/ngXosLib/bower_components/angular/angular.js:4414:53
forEach@/Users/teone/Sites/xos/views/ngXosLib/bower_components/angular/angular.js:336:24
loadModules@/Users/teone/Sites/xos/views/ngXosLib/bower_components/angular/angular.js:4374:12
createInjector@/Users/teone/Sites/xos/views/ngXosLib/bower_components/angular/angular.js:4299:22
workFn@/Users/teone/Sites/xos/views/ngXosLib/bower_components/angular-mocks/angular-mocks.js:2427:60
Expected undefined to be defined.
/Users/teone/Sites/xos/views/ngXosLib/xosHelpers/spec/csrftoken.test.js:17:37

Any idea?

Answers


Ok found the issue.

I was not injecting ngResource module in xos.helpers module. Adding Chrome along with PhantomJs in karma browsers section print out a meaningful error that guide me.


You need use inject for get the angular services and providers, try this:

describe('The xos.helper module', function(){
      var SetCSRFToken, httpProviderObj;

      beforeEach(module('xos.helpers'));

      //Instead 'module' use 'inject'
      beforeEach(inject(function($httpProvider){
        httpProviderObj = $httpProvider;
      }));

      beforeEach(inject(function(_SetCSRFToken_){
        SetCSRFToken = _SetCSRFToken_;
      }));

      it('should exist', () => {
        expect(SetCSRFToken).toBeDefined();
      });

      it('should set SetCSRFToken interceptor', () => {
        expect(httpProviderObj).toBeDefined();
        expect(httpProviderObj.interceptors).toContain('SetCSRFToken');
      });
});

Need Your Help

Android ArrayAdapter does not update ListView

java android listview android-arrayadapter

I have written a small implementation for a listview.