简体   繁体   English

设置Jasmine&Karma以测试Angular

[英]Setting up Jasmine & Karma to test Angular

I'm just trying to get Karma set up to test my Angular app with Jasmine. 我只是想让Karma设置为使用Jasmine测试我的Angular应用程序。 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 : 这是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: 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 is a stripped down version of my app with no dependencies except angular. app_testable.js是我的应用程序的精简版,除了angular之外没有任何依赖项。 Here's my test. 这是我的测试。 I'm just trying to get true to be true . 我只是试图让truetrue

'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? 您正在使用哪个版本的angular? The 'angular' and 'angular-mocks' files should be the same version. “ angular”和“ angular-mocks”文件应为相同版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM