问题描述
您好,我写我的茉莉第一角度测试,但我不断收到错误
------测试开始:文件:C:\\用户\\里根\\文档\\的Visual Studio 2013 \\网站已\\里根\\ testApp \\ TestProject \\ NG-测试\\ MainCtrlSpec.js ------
测试MainCtrl与内嵌模拟:应该有标贴'失败
错误:[$喷油器:unpr]未知提供商:$ scopeProvider< - $范围< - MainCtrl
我试图玩弄它,但我卡住了。如果你看到问题,请让我知道。如果您需要更多code,以及请让我知道,但我认为这个问题是在这两个文件。
MainCtrlSvc.js
///<参考路径=../../脚本/角/ angular.js/>
///<参考路径=../../脚本/角度/角mocks.js/>
///<参考路径=../../脚本/ chartjs / chart.js之/>
///<参考路径=../../脚本/角chart.js之主/距离/角chart.js之/>
///<参考路径=../../脚本/控制器/主controller.js/>
///<参考路径=../../脚本/服务/数据service.js/>
///<参考路径=../../库/茉莉/ jasmine.js/>描述(MainCtrl带内置模拟功能(){
beforeEach(模块(ChartApp)); VAR CTRL,mockDataSrv; beforeEach(模块(函数($提供){
mockDataSrv = {
标签:读,编码,思考编码,reddit的,计算器],
数据:[500,300,300,40,220],
类型:PolarArea
标题:角图Expriment
};
$ provide.value(DataSrv,mockDataSrv);
})); beforeEach(注入(函数($控制器){
CTRL = $控制器(MainCtrl);
})); 它(应该有标贴功能(){
期待(scope.labels).toBeDefined();
});
});
MainCtrl.js
VAR应用= angular.module(ChartApp,[chart.js之]); app.controller(MainCtrl,[$范围
功能($范围,DataSrv){
$ scope.labels = DataSrv.labels;
$ scope.data = DataSrv.data;
$ scope.type = DataSrv.type;
$ scope.title = DataSrv.title;
}
]);
由于没有 $范围
服务, $控制器
提供商不能实例注入 $范围
参数。
您需要提供范围在实例中使用 $控制器
供应商的控制器。你可以注入 $ rootScope
在你的设置,你可以通过做得到一个子作用域 $ rootScope。$新的()
。注入它作为参数传递给 $控制器
contstructor。即 $控制器(MainCtrl,{$范围:适用范围})
,其中范围的,甚至你可以在$ rootScope
即
VAR CTRL,mockDataSrv,范围;
// ...您code
// ...
beforeEach(注(函数($控制器,$ rootScope){
范围= $ rootScope $新的()。 //得到childscope
CTRL = $控制器(MainCtrl,{$范围:适用范围}); //把它作为参数作为$范围的价值
}));
Hello I am writing my first angular test with Jasmine but I keep getting the error
------ 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
/// <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
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;
}
]);
Since there is no $scope
service, $controller
provider cannot instantiate the injected $scope
argument.
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.
i.e
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
}));
这篇关于角单元测试未知提供商:$ scopeProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!