[英]Angular JS: Error: [$injector:unpr] Unknown provider: $scopeProvider
I'm trying to write some unit tests for my app, but I'm running into an issue where I'm getting this error: 我正在尝试为我的应用程序编写一些单元测试,但是遇到一个出现此错误的问题:
Chrome 46.0.2490 (Mac OS X 10.10.5) app.customer "before each" hook for "should be created successfully" FAILED Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- Customer Chrome 46.0.2490(Mac OS X 10.10.5)app.customer“应在每个之前”钩子应成功创建“失败”错误:[$ injector:unpr]未知提供程序:$ scopeProvider <-$ scope <-Customer
My test.js
file looks like this: 我的test.js
文件如下所示:
describe('app.customers', function() {
var controller;
beforeEach(function() {
module('app', function($provide) {});
specHelper.injector(function($controller, $q, $rootScope, DataService) {});
});
beforeEach(function() {
sinon.stub(DataService, 'getCustomers', function() {
var deferred = $q.defer();
deferred.resolve(mockData.getMockCustomers());
return deferred.promise;
});
controller = $controller('Customer');
$rootScope.$apply();
});
describe('Customer Controller', function() {
it('should be created successfully', function() {
expect(controller).to.be.defined;
});
});
specHelper.verifyNoOutstandingHttpRequests();
});
I'm not sure why it's looking for $scopeProvider
as I'm not referencing $scope.provider
in my test. 我不确定为什么要寻找$scopeProvider
因为我没有在测试中引用$scope.provider
。
I'm pretty new to unit testing in general, so any help with this would be appreciated. 一般而言,我对单元测试还很陌生,因此可以提供任何帮助。 Thanks in advance! 提前致谢!
Thanks in advance! 提前致谢!
You have to initialize controller with a local scope. 您必须使用本地范围初始化控制器。
beforeEach(function() {
sinon.stub(DataService, 'getCustomers', function() {
var deferred = $q.defer();
deferred.resolve(mockData.getMockCustomers());
return deferred.promise;
});
// inject local scope into controller
controller = $controller('Customer', {$scope: $rootScope});
$rootScope.$apply();
});
My guess is that your Customer controller is requiring $scope, but nothing is providing it. 我的猜测是您的客户控制器需要$ scope,但没有提供它。 This is done for you automatically when running in an app, but something you need to provide manually when unit testing through $controller. 在应用程序中运行时,此操作会自动为您完成,但是在通过$ controller进行单元测试时,您需要手动提供一些操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.