2014-12-24 37 views
9

这里是我的2个文件和错误。我正在运行Karma,这是我的karma.conf.js:https://gist.github.com/devanp92/a87c0bcc2bf5b8e17f64。运行“业力开始”后,我得到这个错误。这是一个非常简单的测试文件(或者我假设),我仍然遇到这些错误。错误Karma'undefined'范围变量在beforeEach

Main.js - 这是一个控制器

angular.module('SSLApp').controller('MainCtrl', 
    function($scope, $rootScope) { 
    $scope.thing = 1; 
}); 

MainSpec.js - 这是我的测试文件

describe('MainCtrl', function() { 
var controller, scope; 

beforeEach(angular.module('SSLApp')); 

beforeEach(inject(function($controller, $rootScope) { 
    scope = $rootScope.$new(); 
    controller = $controller('MainCtrl', { 
     $scope: scope 
    }); 
})); 

it('should have scope to be defined', function(scope) { 
    expect(scope).toBeDefined(); 
}); 

}); 

错误!它看起来像它被称为undec中var MainCtrl = $控制器...

TypeError: 'undefined' is not a function (evaluating 'queueableFn.fn.call(self.userContext)') 
     at /Users/BLAH/Documents/node_modules/karma-jasmine/lib/adapter.js:184 
     at `http://localhost:9876/karma.js:185` 
     at `http://localhost:9876/context.html:53` 
    Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined 
    http://errors.angularjs.org/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined 
     at assertArg (/Users/BLAH/Documents/node_modules/angular/angular.js:1577) 
     at assertArgFn (/Users/BLAH/Documents/node_modules/angular/angular.js:1588) 
     at /Users/BLAH/Documents/node_modules/angular/angular.js:8418 
     at /Users/BLAH/Documents/test/controllers/MainSpec.js:9 
     at invoke (/Users/BLAH/Documents/node_modules/angular/angular.js:4182) 
     at workFn (/Users/BLAH/Documents/node_modules/angular-mocks/angular-mocks.js:2350) 
     at /Users/BLAH/Documents/node_modules/karma-jasmine/lib/adapter.js:184 
     at http://localhost:9876/karma.js:185 
     at http://localhost:9876/context.html:53 
    undefined 
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 
PhantomJS 1.9.8 (Mac OS X): Executed 1 of 1 (1 FAILED) ERROR (4.999 secs/5.012 secs) 

回答

14

你在测试代码中有两个错误。

首先你用错了模块函数。 angular.module()函数提供了一个真正的框架模块,而 简单模块()angular.mock.module()的别名,您应该在测试中使用它。所以,你应该写你的beforeEach功能如下:

beforeEach(module('SSLApp')); 

再说了,你一个参数定义的测试案例的功能,但它应该是参数的。 scope变量可以从外部范围访问。

it('should have scope to be defined', function() { 
    expect(scope).toBeDefined(); 
}); 
+0

谢谢!这解决了它! – user2874945

+3

对于那些使用webpack配置运行业务的人,我推荐使用'angular.mock.module'而不是'module',因为全局'module'将指向节点的模块。 – brunocoelho