1

I'm just trying to get Karma set up to test my Angular app with Jasmine. I'm getting this error:

INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 30.0.1599 (Mac OS X 10.9.0)]: Connected on socket pxDaOUXMfGXOVL7ZOuPb
Chrome 30.0.1599 (Mac OS X 10.9.0) ItemCtrl should work FAILED
    TypeError: Object [object Object] has no method 'off'
        at null.<anonymous> (/Users/macbookpro/Sites/groceries/spec/angular-mocks.js:1917:36)

Here's that function in angular-mocks.js:

afterEach(function() {
    var injector = currentSpec.$injector;

    currentSpec.$injector = null;
    currentSpec.$modules = null;
    currentSpec = null;

    if (injector) {
      injector.get('$rootElement').off();
      injector.get('$browser').pollFns.length = 0;
    }

    angular.mock.clearDataCache();

    // clean up jquery's fragment cache
    angular.forEach(angular.element.fragments, function(val, key) {
      delete angular.element.fragments[key];
    });

    MockXhr.$$lastInstance = null;

    angular.forEach(angular.callbacks, function(val, key) {
      delete angular.callbacks[key];
    });
    angular.callbacks.counter = 0;
  });

I think I'm just requiring files in the wrong order in my karma config. Here's my file structure:

karama.config.js is in the the same directory /spec as the files it's requiring:

// list of files / patterns to load in the browser
    files: [
      'angular.min.js',
      'angular-mocks.js',
      'app_testable.js',
      'app_spec.js'
    ],

app_testable.js is a stripped down version of my app with no dependencies except angular. Here's my test. I'm just trying to get true to be true.

'use strict';

describe('ItemCtrl', function(){
  var scope;
  beforeEach(angular.mock.module('Grocerease'));
  beforeEach(angular.mock.inject(function($rootScope, $controller){
      scope = $rootScope.$new();
      $controller('ItemCtrl', {$scope: scope});
    })
  );

  it('should work', function(){
    expect(true).toBe(true);
  });

});

How do I need to set up karma so that it is able to run the tests correctly?

7
  • Are you using jquery? Commented Nov 5, 2013 at 5:33
  • No. The only files I'm using are listed above. As an aside, I didn't include angular-resource because I'm not using the router module. Is that required? Commented Nov 5, 2013 at 5:38
  • angular-resource is not for router module but to handle "resources" from REST APIs. Recheck that you are not missing any dependency that might be using at index.html Commented Nov 5, 2013 at 5:48
  • btw, did you check at console of the browser for any additional info? Commented Nov 5, 2013 at 5:51
  • Indeed I did. Same error as above, referencing the same line in angular-mocks. Commented Nov 5, 2013 at 6:02

1 Answer 1

5

The problem seems like a dependency was incorrectly loaded.

What version of angular are you working with? The 'angular' and 'angular-mocks' files should be the same version.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.