我有一个非常简单的过滤器,可以为我的模板构建URL:
angular.module('deAdmin.filters.shiurViewLink', []).filter('shiurViewLink', ['configService', function(configService) {
return function(shiur) {
if (shiur == null)
return "#";
return configService.getAppRoot() + "/shiurim/" + shiur.id + "/";
}
}]);
引用的configService
:
angular.module('deAdmin.services.configService', []).factory('configService', [function() {
var service = {};
service.getAppRoot = function() {
return "/admin";
};
service.getStaticPrefix = function() {
return "/static/admin";
}
return service;
}]);
以下是我对过滤器的测试:
'use strict';
describe('shiurViewLink', function() {
var $filter, filter;
beforeEach(function() {
module('deAdmin.filters.shiurViewLink');
inject(function($injector) {
$filter = $injector.get('$filter');
filter = $filter('shiurViewLink');
});
});
it("should properly generate the shiur view link", function() {
expect(filter({ id: "abcdefg" })).toBe('/admin/');
});
});
我在Karma / Jasmine测试运行中看到以下错误:
Error: [$injector:unpr] Unknown provider: configServiceProvider <- configService <- shiurViewLinkFilter
http://errors.angularjs.org/1.2.4/$injector/unpr?p0=configServiceProvider%20%3C-%20configService%20%3C-%20shiurViewLinkFilter
at /home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:78:12
at /home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3479:19
at Object.getService [as get] (/home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3606:39)
at /home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3484:45
at getService (/home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3606:39)
at Object.invoke (/home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3628:13)
at /home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3485:37
at Object.getService [as get] (/home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:3606:39)
at /home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular/angular.js:13687:24
at /home/naftuli/Documents/Work/PyCharm/rtshiurim/test/js/admin/filters/shiur-view-link-test.js:10:22
Error: Declaration Location
at window.inject.angular.mock.inject (/home/naftuli/Documents/Work/PyCharm/rtshiurim/bower_components/angular-mocks/angular-mocks.js:2086:25)
at null.<anonymous> (/home/naftuli/Documents/Work/PyCharm/rtshiurim/test/js/admin/filters/shiur-view-link-test.js:8:9)
TypeError: undefined is not a function
at null.<anonymous> (/home/naftuli/Documents/Work/PyCharm/rtshiurim/test/js/admin/filters/shiur-view-link-test.js:15:16)
如何在Jasmine测试中处理依赖项?它似乎无法找到我的服务,但我很确定该服务正在导入。
答案 0 :(得分:1)
您的configService
模块应该deAdmin.services.configService
作为模块依赖项,因为configService
和shiurViewLink
位于不同的模块中。
angular.module('deAdmin.filters.shiurViewLink', ['deAdmin.services.configService']).filter('shiurViewLink', ['configService', function(configService) {
return function(shiur) {
if (shiur == null)
return "#";
return configService.getAppRoot() + "/shiurim/" + shiur.id + "/";
}
}]);