问题描述
你好,我正在用 Jasmine 编写我的第一个角度测试,但我不断收到错误
Hello I am writing my first angular test with Jasmine but I keep getting the error
------ 测试开始:文件:C:\Users\Regan\Documents\Visual Studio 2013\WebSites\Regan\testApp\TestProject\ng-tests\MainCtrlSpec.js ------使用内联模拟测试MainCtrl:应该有标签"失败错误:[$injector:unpr] 未知提供者:$scopeProvider
------ Test started: File: C:\Users\Regan\Documents\Visual Studio 2013\WebSites\Regan\testApp\TestProject\ng-tests\MainCtrlSpec.js ------Test 'MainCtrl with inline mock:should have lables' failed Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- MainCtrl
我试过玩弄它,但被卡住了.如果您看到问题,请告诉我.如果您还需要更多代码,请告诉我,但我认为问题出在这两个文件中.
I have tried playing around with it but am stuck. If you see the problem please let me know. If you need more code as well please let me know but I think the problem is in these two files.
MainCtrlSvc.js
MainCtrlSvc.js
/// <reference path="../../Scripts/angular/angular.js" />
/// <reference path="../../Scripts/angular/angular-mocks.js" />
/// <reference path="../../Scripts/chartjs/Chart.js" />
/// <reference path="../../Scripts/angular-chart.js-master/dist/angular-chart.js" />
/// <reference path="../../Scripts/controller/main-controller.js" />
/// <reference path="../../Scripts/service/data-service.js" />
/// <reference path="../../libs/jasmine/jasmine.js" />
describe("MainCtrl with inline mock", function () {
beforeEach(module("ChartApp"));
var ctrl, mockDataSrv;
beforeEach(module(function($provide) {
mockDataSrv = {
labels: ["Reading", "Coding", "Thinking About Coding", "Reddit", "StackOverflow"],
data: [500, 300, 300, 40, 220],
type: "PolarArea",
title: "Angular Chart Expriment"
};
$provide.value("DataSrv", mockDataSrv);
}));
beforeEach(inject(function ($controller) {
ctrl = $controller("MainCtrl");
}));
it("should have lables", function () {
expect(scope.labels).toBeDefined();
});
});
MainCtrl.js
MainCtrl.js
var app = angular.module("ChartApp", ["chart.js"]);
app.controller("MainCtrl", ["$scope",
function ($scope, DataSrv) {
$scope.labels = DataSrv.labels;
$scope.data = DataSrv.data;
$scope.type = DataSrv.type;
$scope.title = DataSrv.title;
}
]);
推荐答案
由于没有 $scope
服务,$controller
provider 无法实例化注入的 $范围
参数.
Since there is no $scope
service, $controller
provider cannot instantiate the injected $scope
argument.
您需要在使用 $controller
提供程序实例化控制器时提供范围.你可以在你的 setUp 中注入 $rootScope
并且你可以通过执行 $rootScope.$new()
来获得一个子作用域.将其作为参数注入 $controller
构造函数.即 $controller("MainCtrl", {$scope:scope })
其中 scope 是新的子作用域,即使您可以传入 $rootScope.
You need to provide the scope while instantiating a controller using the $controller
provider. You can inject $rootScope
in your setUp and you can get a child scope by doing $rootScope.$new()
. Inject it as an argument to the $controller
contstructor. i.e $controller("MainCtrl", {$scope:scope })
where scope is the new child scope of even you can pass in the $rootScope.
即
var ctrl, mockDataSrv, scope;
//... Your code
//...
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new(); //get a childscope
ctrl = $controller("MainCtrl", {$scope:scope }); //Pass it as argument as $scope's value
}));
这篇关于Angular 单元测试未知提供者:$scopeProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!