错误beforeEach噶'不确定'的范围变量 [英] Error Karma 'undefined' scope variable in beforeEach

查看:342
本文介绍了错误beforeEach噶'不确定'的范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的2个文件和错误。我正在噶,这里是我的karma.conf.js:<一href=\"https://gist.github.com/devanp92/a87c0bcc2bf5b8e17f64\">https://gist.github.com/devanp92/a87c0bcc2bf5b8e17f64.运行开始因缘之后,我得到这个错误。这是一个非常简单的测试文件(或因此我认为),我仍然得到这些错误。

Main.js - 这是一个控制器

  angular.module('SSLApp')。控制器('MainCtrl',
  功能($范围,$ rootScope){
    $ scope.thing = 1;
});

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

 描述('MainCtrl',函数(){
VAR控制器,范围;beforeEach(angular.module('SSLApp'));beforeEach(注(函数($控制器,$ rootScope){
    范围= $ rootScope $新的()。
    控制器= $控制器('MainCtrl',{
        $适用范围:适用范围
    });
}));它('应该有域中定义'功能(适用范围){
    期待(范围).toBeDefined();
});});

错误!它看起来就像是被称为在var未定义MainCtrl = $控制器...

 类型错误:未定义是不是(评估'queueableFn.fn.call(self.userContext)')函数
        在/Users/BLAH/Documents/node_modules/karma-jasmine/lib/adapter.js:184
        在`HTTP://本地主机:9876 / karma.js:185`
        在`HTTP://本地主机:9876 / context.html:53`
    错误:[NG:AREQ]参数'MainCtrl不是一个函数,得到了不确定
    http://errors.angularjs.org/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
        在assertArg(/Users/BLAH/Documents/node_modules/angular/angular.js:1577)
        在assertArgFn(/Users/BLAH/Documents/node_modules/angular/angular.js:1588)
        在/Users/BLAH/Documents/node_modules/angular/angular.js:8418
        在/Users/BLAH/Documents/test/controllers/MainSpec.js:9
        在调用(/Users/BLAH/Documents/node_modules/angular/angular.js:4182)
        在workFn(/Users/BLAH/Documents/node_modules/angular-mocks/angular-mocks.js:2350)
        在/Users/BLAH/Documents/node_modules/karma-jasmine/lib/adapter.js:184
        在http://本地主机:9876 / karma.js:185
        在http://本地主机:9876 / context.html:53
    未定义
    错误:超时 - 异步回调不被jasmine.DEFAULT_TIMEOUT_INTERVAL指定的时间内调用。
PhantomJS 1.9.8键(Mac OS X):处决1(1失败)错误(4.999秒/ 5.012秒)


解决方案

您在您的测试code两个错误。

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

  beforeEach(模块('SSLApp'));

此外,你定义一个参数测试的情况下的功能,但它应是无参数。在范围变量是从外部范围访问。

 它('应该有域中定义',函数(){
    期待(范围).toBeDefined();
});

Here are my 2 files and error. I am running Karma, here is my karma.conf.js: https://gist.github.com/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.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)

解决方案

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

这篇关于错误beforeEach噶'不确定'的范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆