angularjs - Karma - 未知提供商 : $scopeProvider

标签 angularjs testing karma-jasmine

我得到 - 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);
    })
   )
})
});

但我不知道为什么。我已经注入(inject)了范围(据我所知)并在我的 karma.conf.js 文件中包含了 Angular 模拟。

最佳答案

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

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

只需删除它,它就可以工作了:

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

就像错误状态一样,没有$scopeProvider。测试时,您必须手动创建范围并分配它们,就像您在创建 Controller 时所做的那样:

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

关于angularjs - Karma - 未知提供商 : $scopeProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285654/

相关文章:

javascript - Sinon TypeError : Attempted to wrap undefined property getInternationals as function

ruby-on-rails - 为深层嵌套模型创建工厂

angular - 在项目中的所有应用程序上运行 ng test

angular - 单击事件发生时如何对下拉菜单进行单元测试

javascript - angularjs 中有多少种限制可用

javascript - Angularjs:使用路由 - 自定义指令未编译或无法识别

javascript - AngularJS 将部分 Controller 移至服务

PHP - Simpletest - 如何测试 "included"类

angular - 如何测试 Angular - catchError 运算符 'rxjs'

angularjs - angularjs ng-click 可以在捕获阶段处理事件吗?