我正在尝试将服务注入到Angular单元测试用例中,但是会出现此错误,
[$ injector:unpr] http://errors.angularjs.org/1.4.7/ $ injector / unpr?p0 = FactsSvcProvider%20%3C-%20FactsSvc
我理解错误,我无法弄明白为什么它无法解决依赖?
以下是代码片段
app.js
angular.module('numberApp', ['ngAnimate', 'ngResource', 'ui.router', 'ngMock'])
.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
//config code...
})
SpecRunner.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner v2.4.1</title>
<link rel="shortcut icon" type="image/png" href="scripts/libraries/jasmine-2.4.1/jasmine_favicon.png">
<link rel="stylesheet" href="scripts/libraries/jasmine-2.4.1/jasmine.css">
<script src="scripts/libraries/jasmine-2.4.1/jasmine.js"></script>
<script src="scripts/libraries/jasmine-2.4.1/jasmine-html.js"></script>
<script src="scripts/libraries/jasmine-2.4.1/boot.js"></script>
<!-- include source files here... -->
<script src="scripts/libraries/angular.min.js"></script>
<script src="scripts/libraries/angular-mocks.js"></script>
<script src="scripts/libraries/angular-resource.min.js"></script>
<script src="scripts/libraries/angular-ui-router.min.js"></script>
<script src="scripts/app.js"></script>
<script src="scripts/services/facts-service.js"></script>
<script src="scripts/controllers/facts-controller.js"></script>
<script src="scripts/services/api-interceptor.service.js"></script>
<!-- include spec files here... -->
<script src="scripts/controllers/facts-controller.spec.js"></script>
</head>
<body>
</body>
</html>
事实-controller.spec.js
describe('Test Controller', function () {
var scope, controller, FactsSvc, $q;
beforeEach(function() {
angular.module('numberApp', ['ngAnimate', 'ngResource', 'ui.router', 'ngMock']);
});
beforeEach(inject(function (_$rootScope_, $controller, _$q_, _factsSvc_) {
scope = _$rootScope_.$new();
$q = _$q_;
FactsSvc = _factsSvc_;
controller = $controller('factsCtrl', {
$scope: scope,
FactsSvc: FactsSvc
});
}));
it('should call the generateMathFacts method in facts service to' +
' retrieve the facts',
function() {
var num = 20,
response = {
text: 'This is a fact'
},
dfd = $q.defer(),
promise = dfd.promise();
dfd.resolve(response);
spyOn(FactsSvc, 'getMathFacts').andReturns(promise);
scope.generateMathFact(num);
expect(FactsSvc.getMathFacts).toHaveBeenCalled();
});
});
我收到此错误
Error: [$injector:unpr] http://errors.angularjs.org/1.4.7/$injector/unpr?p0=factsSvcProvider%20%3C-%20factsSvc
at Error (native)
at http://localhost:8080/scripts/libraries/angular.min.js:6:416
at http://localhost:8080/scripts/libraries/angular.min.js:40:409
at Object.d [as get] (http://localhost:8080/scripts/libraries/angular.min.js:38:394)
at http://localhost:8080/scripts/libraries/angular.min.js:40:483
at d (http://localhost:8080/scripts/libraries/angular.min.js:38:394)
at Object.e [as invoke] (http://localhost:8080/scripts/libraries/angular.min.js:39:161)
at Object.workFn (http://localhost:8080/scripts/libraries/angular-mocks.js:2810:20)
at attemptSync (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1886:24)
at QueueRunner.run (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1874:9)
Error: Declaration Location
at window.inject.angular.mock.inject (http://localhost:8080/scripts/libraries/angular-mocks.js:2778:25)
at Suite.<anonymous> (http://localhost:8080/scripts/controllers/facts-controller.spec.js:8:14)
at addSpecsToSuite (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:833:25)
at Env.describe (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:802:7)
at jasmineInterface.describe (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:3375:18)
at http://localhost:8080/scripts/controllers/facts-controller.spec.js:1:1
TypeError: Cannot read property 'defer' of undefined
TypeError: Cannot read property 'defer' of undefined
at Object.<anonymous> (http://localhost:8080/scripts/controllers/facts-controller.spec.js:25:15)
at attemptSync (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1886:24)
at QueueRunner.run (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1874:9)
at QueueRunner.execute (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1859:10)
at Spec.Env.queueRunnerFactory (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:697:35)
at Spec.execute (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:359:10)
at Object.fn (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:2479:37)
at attemptAsync (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1916:24)
at QueueRunner.run (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1871:9)
at QueueRunner.execute (http://localhost:8080/scripts/libraries/jasmine-2.4.1/jasmine.js:1859:10)
控制器代码
"use strict"
angular.module('numberApp')
.controller('factsCtrl', function($scope, factsSvc, message, $rootScope) {
//logic...
})
服务代码
"use strict"
angular.module('numberApp')
.service('factsSvc', function($resource, resourceAPI) {
//logic...
}
请任何帮助将不胜感激。