错误:未知提供者:$ scopeProvider< - 范围在karma-jasmine中

时间:2016-11-24 11:00:21

标签: angularjs karma-jasmine

我的控制器:

angular.module('dr.search.components',[])
    .controller('HomeCtrl', function ($scope, $state, $timeout, $interval, $rootScope) {
    // code
});

测试代码(内部描述):

var HomeController, $controller;
var scope, $state, $timeout, $interval,$rootScope;

beforeEach(module('dr.search.components'));
beforeEach(module('ui.router'));

beforeEach(inject(function(_$controller_, _$scope_, _$state_, _$timeout_, _$interval_, _$rootScope_) {
    $controller = _$controller_;
    scope = _$rootScope_.$new();
    $state = _$state_;
    $timeout = _$timeout_;
    $interval = _$interval_;
    $rootScope = _$rootScope_;

    HomeController = $controller('HomeCtrl', {$scope: scope, $state: $state, $timeout: $timeout, $interval: $interval, $rootScope: $rootScope});

}));

// Verify our controller exists
it('Home Controller should be defined', function() {
    expect(HomeController).toBeDefined();
});

我正在测试我的控制器,如果它存在与否。它向我展示了未知提供者:$ scopeProvider< - $ scope。

1 个答案:

答案 0 :(得分:2)

beforeEach删除$ scope。

beforeEach(inject(function(_$controller_, _$state_, _$timeout_, _$interval_, _$rootScope_) {
    ...
}));