Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $rootScope在使用Karma进行测试时未定义_Angularjs_Unit Testing_Karma Runner - Fatal编程技术网

Angularjs $rootScope在使用Karma进行测试时未定义

Angularjs $rootScope在使用Karma进行测试时未定义,angularjs,unit-testing,karma-runner,Angularjs,Unit Testing,Karma Runner,我正试图为一个角度项目编写一些单元测试。我已经跟随了一些关于测试控制器的例子,但我总是遇到同样的问题:$rootScope似乎没有定义 My test.js: describe("Unit: BodyCtrl", function() { var scope; beforeEach(function($rootScope, $controller) { scope = $rootScope.$new(); }); it('foo should

我正试图为一个角度项目编写一些单元测试。我已经跟随了一些关于测试控制器的例子,但我总是遇到同样的问题:$rootScope似乎没有定义

My test.js:

describe("Unit: BodyCtrl", function() {

    var scope;

    beforeEach(function($rootScope, $controller) {
        scope = $rootScope.$new();
    });

    it('foo should be foo', function() {
        expect("foo").toBe("foo");
    });
})
错误是:

TypeError: Cannot read property '$new' of undefined'

我已经在karma.conf.js中包含了所有角度文件。我通过sshfs将主应用程序从服务器安装到本地Ubuntu 14.04,并且Karma安装在所述本地机器上,这会导致问题吗?

您需要注入它。在每个之前的
中使用
注入
,否则它只是在函数范围中定义的变量,没有定义任何值

例如:-

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