Karma - 未知提供者: $scopeProvider

4

当运行以下测试时,我收到了Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope错误:

describe('mainCtrl', function () {

beforeEach(module('app'));
var controller, scope;
var window = {
    open: function (url, target, specs) {
        var spec, specKey;
        this.href = url;
        this.target = target;
        // Parse through the spec string to grab the parameters you passed through
        var specArray = specs.split(',');
        for (specKey in specArray) {
            spec = specArray[specKey].split('=');
            this[String.trim(spec[0])] = String.trim(spec[1]);
        }
    }
};

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

describe('$scope.popup1', function () {

    it('should open a popup window when ISIN hyperlink is clicked within grid, passing ISIN object s values to shareDataService', inject(function ($window, $scope) {
        spyOn($window, 'open').and.callFake(function () {
            return true;
        });
        scope.popup1()
        expect(window.href).toEqual("views/Box_Ladder.html");
        expect(window.target).toEqual("_blank");
        expect(window.height).toEqual(400);
        expect(window.width).toEqual(700);
    })
   )
})
});

但我不知道为什么。 我已经注入了作用域(据我所见),并在我的karma.conf.js文件中包含了angular-mocks。

1个回答

4

这是因为你试图将$scope注入到it函数中:

it('should open a popup window ...', inject(function ($window, $scope)

只需将其删除,然后它就应该正常工作:

it('should open a popup window ...', inject(function ($window)

就像错误提示所述,没有$scopeProvider。在测试时,您必须手动创建范围并分配它们,就像创建控制器时所做的那样:
scope = $rootScope.$new();
controller = $controller('mainCtrl', {$scope: scope});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接