简体   繁体   中英

Setting up Jasmine & Karma to test Angular

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?

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.

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