最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error Karma 'undefined' scope variable in beforeEach - Stack Overflow

programmeradmin2浏览0评论

Here are my 2 files and error. I am running Karma, here is my karma.conf.js: . After running 'karma start' I get this error. It is a very simple test file (or so I assume) and I am still getting these errors.

Main.js - This is a controller

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

MainSpec.js - This is my test file

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();
});

});

ERROR! It looks like it is being called undefined in the var MainCtrl = $controller...

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
    .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)

Here are my 2 files and error. I am running Karma, here is my karma.conf.js: https://gist.github./devanp92/a87c0bcc2bf5b8e17f64. After running 'karma start' I get this error. It is a very simple test file (or so I assume) and I am still getting these errors.

Main.js - This is a controller

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

MainSpec.js - This is my test file

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();
});

});

ERROR! It looks like it is being called undefined in the var MainCtrl = $controller...

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/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)
Share Improve this question asked Dec 24, 2014 at 3:06 user2874945user2874945 3087 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

You have two mistakes in your test code.

First of all you use wrong module function. The angular.module() function provides a real framework module while simple module() is an alias for angular.mock.module() which you should use in tests. So you ought to write your beforeEach function as follows:

beforeEach(module('SSLApp'));

Besides, you defined the test case function with a parameter but it should be parameterless. The scope variable is accessible from the outer scope.

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

评论列表(0)

  1. 暂无评论