1

我只是想让 Karma 设置好用 Jasmine 测试我的 Angular 应用程序。我收到此错误:

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)

这是 中的函数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;
  });

我想我只是在我的业力配置中要求文件顺序错误。这是我的文件结构:

karama.config.js与所需文件位于同一目录/spec中:

// 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是我的应用程序的精简版本,除了 angular 之外没有任何依赖项。这是我的测试。我只是想true成为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);
  });

});

我需要如何设置 karma 才能正确运行测试?

4

1 回答 1

5

问题似乎是错误加载了依赖项。

您正在使用哪个版本的角度?'angular' 和 'angular-mocks' 文件应该是相同的版本。

于 2013-11-06T02:56:38.770 回答