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

Angularjs $scope在使用Karma进行测试时未定义,angularjs,ionic-framework,karma-jasmine,Angularjs,Ionic Framework,Karma Jasmine,我是一个测试AngularJS的完全初学者,所以任何建议都非常感谢 当我尝试运行Karma Start时,我遇到了一个错误,看起来像这样 Chrome 54.0.2840 (Mac OS X 10.11.6) DashboardCtrl Should Exist FAILED Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- DashboardCtrl http://errors.angular

我是一个测试AngularJS的完全初学者,所以任何建议都非常感谢

当我尝试运行Karma Start时,我遇到了一个错误,看起来像这样

Chrome 54.0.2840 (Mac OS X 10.11.6) DashboardCtrl Should Exist FAILED
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- DashboardCtrl
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20DashboardCtrl
    at node_modules/angular/angular.js:68:12
    at node_modules/angular/angular.js:4511:19
    at Object.getService [as get] (node_modules/angular/angular.js:4664:39)
    at node_modules/angular/angular.js:4516:45
    at getService (node_modules/angular/angular.js:4664:39)
    at injectionArgs (node_modules/angular/angular.js:4688:58)
    at Object.instantiate (node_modules/angular/angular.js:4730:18)
    at $controller (node_modules/angular/angular.js:10369:28)
    at node_modules/angular-mocks/angular-mocks.js:2221:12
    at Object.<anonymous> (www/tests/dashboard.spec.js:9:19)
Error: Declaration Location
    at window.inject.angular.mock.inject (node_modules/angular-mocks/angular-mocks.js:3047:25)
    at Suite.<anonymous> (www/tests/dashboard.spec.js:7:13)
    at www/tests/dashboard.spec.js:1:1
Expected undefined to be defined.
    at Object.<anonymous> (www/tests/dashboard.spec.js:13:25)
angular.module('myApp.controllers', [])
.controller('DashboardCtrl', function($rootScope, $scope, API, $ionicModal, $window, $ionicLoading) {
包含以下部分内容和所有必需的结束标记;)

我的测试代码如下所示

describe('DashboardCtrl', function(){
var $controller, DashboardCtrl;

beforeEach(angular.mock.module('ui.router'));
beforeEach(angular.mock.module('myApp.controllers'));

beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
    DashboardCtrl = $controller('DashboardCtrl', {});
}));

it('Should Exist', function(){
    expect(DashboardCtrl).toBeDefined();
})
});

我已经看到了一些关于这个的其他帖子,但我就是不知道我错在哪里了,谢谢大家的帮助

您忘记了创建和注入
$scope

describe('DashboardCtrl', function(){
    var $scope,
        DashboardCtrl;

    beforeEach(angular.mock.module('ui.router'));
    beforeEach(angular.mock.module('myApp.controllers'));

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

        DashboardCtrl = $controller('DashboardCtrl', {
            $scope: $scope
        });
    }));

    it('Should Exist', function(){
        expect(DashboardCtrl).toBeDefined();
    })
});

您必须将依赖项注入controllerThanks,这样就消除了错误,但是,它已经停止了我的$rootScope函数在控制器中的工作,只是想知道我是否也需要注入rootScope?如果是这样的话,是否会有不同的做法?如果(!$rootScope.isSessionActive()){$window.location.href=('');}以上是失败的代码,这是在services.js$rootScope.isSessionActive=函数()中定义的({返回$window.localStorage.token&&$window.localStorage.username?true:false;}@BRollinson你试过我的解决方案吗?