这是我的考试。我收到$httpBackend
未定义的错误。
describe("Objects Service", function () {
var $httpBackend, $rootScope, scope, datacontext, config;
beforeEach(function () {
module('agApp');
});
beforeEach(inject(function ($rootScope, _$httpBackend_, _datacontext_, _config_) {
scope = $rootScope.$new();
datacontext = _datacontext_;
$httpBackend = _$httpBackend_;
config = _config_;
}));
it("should call right API adress to get all objects", function () {
$httpBackend.whenGET('/api/objects').respond(200);
datacontext.objects.getObjects(function (data) {
$httpBackend.flush();
});
});
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectations();
$httpBackend.verifyNoOutstandingRequest();
});
});
3个规格,1个失败 规格清单|故障 对象服务应该调用正确的API地址来获取所有对象 错误:[$ injector:unpr]未知提供者:configProvider< - config http://errors.angularjs.org/1.3.9/ $注射器/ unpr?P0 = configProvider%20%3 C-%20config 错误:[$ injector:unpr]未知提供者:configProvider< - config http://errors.angularjs.org/1.3.9/ $注射器/ unpr?P0 = configProvider%20%3 C-%20config 在http://localhost/WebRenter/Scripts/vendor/angular.js:64:20 在http://localhost/WebRenter/Scripts/vendor/angular.js:3995:21 at Object.getService [as get](http://localhost/WebRenter/Scripts/vendor/angular.js:4142:53) 在http://localhost/WebRenter/Scripts/vendor/angular.js:4000:47 at getService(http://localhost/WebRenter/Scripts/vendor/angular.js:4142:53) at Object.invoke(http://localhost/WebRenter/Scripts/vendor/angular.js:4174:13) at Object.workFn(http://localhost/WebRenter/bower_components/angular-mocks/angular-mocks.js:2436:20) at attemptSync(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1741:24) 在QueueRunner.run(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1729:9) 在QueueRunner.execute(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10) 错误:声明位置 在window.inject.angular.mock.inject(http://localhost/WebRenter/bower_components/angular-mocks/angular-mocks.js:2407:25) 在套房。 (http://localhost/WebRenter/Test/objects/repository.objects.Spec.js:9:16) at addSpecsToSuite(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:725:25) 在Env.describe(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:695:7) 在jasmineInterface.describe(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2969:18) 在http://localhost/WebRenter/Test/objects/repository.objects.Spec.js:4:1 TypeError:无法读取属性'当GET'未定义的 TypeError:无法读取属性'当GET'未定义的 在对象。 (http://localhost/WebRenter/Test/objects/repository.objects.Spec.js:20:20) at attemptSync(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1741:24) 在QueueRunner.run(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1729:9) 在QueueRunner.execute(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10) 在Spec.Env.queueRunnerFactory(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:608:35) 在Spec.execute(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:346:10) 在Object.fn(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2059:43) at attemptAsync(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1771:24) 在QueueRunner.run(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1726:9) 在QueueRunner.execute(http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10)
这是datacontext:
angular.module("agApp").factory("datacontext", ['$http', '$q', 'repositories', function ($http, $q, repositories) {
var RepoNames = ['objects', 'images', 'objectattributes', 'info', 'units', 'unitattributes']
var service = {
}
init();
return service;
function init() {
defineLazyLoadedRepos();
}
function defineLazyLoadedRepos() {
RepoNames.forEach(function (name) {/**/
Object.defineProperty(service, name, { //
configurable: true,
get: function () {///
var repo = repositories.getRepo(name); //samo prvi put a poslije može confugurable false
Object.defineProperty(service, name, {
value: repo,
configurable: false,
enumerable: true
});
return repo;
}///
});
//
}); /**/
}
} ]);
这是objects.repository文件的开始:
(function () {
var serviceId = 'repository.objects';
angular.module("agApp").factory(serviceId, ['$http', '$q', '$cacheFactory', 'repository.abstract', 'config', function ($http, $q, $cacheFactory, AbstractRepository, config) {
var entityName = 'objects';
var apiRootUrl = ROOT + "api/";
var cache = $cacheFactory("objectCache");
var cacheOn = config.cache.globalCache && config.cache.objectCache;
var _getObjects = function (object) {
var deferred = $q.defer();
$http.get(apiRootUrl + "objects").then(function (data, status, headers, config) {
deferred.resolve(data.data);
}, function (response) {
self._queryFailed(response);
deferred.reject();
});
return deferred.promise;
}
这是配置:
(function () {
'use strict'
var agApp = angular.module('agApp');
var apiUrl = "api/";
//ROOT je definiran na layoutu
var viewsUrl = ROOT + 'App/Scripts/views';
var config = {
version: '0.0.1',
apiUrl: apiUrl,
viewsUrl: viewsUrl,
root:ROOT,
cache:{
globalCache:true,
objectCache:true,
objectAttrCache:true,
unitCache:true
}
};
agApp.value('config', config);
agApp.config(['$logProvider','$locationProvider', function ($logProvider,$locationProvider) {
$locationProvider.html5Mode({
enabled: true,
});
if ($logProvider.debugEnabled) {
$logProvider.debugEnabled(true);
}
} ]);
})();
答案 0 :(得分:0)
根据您提供的信息,首先查看您的第一条错误消息所链接的页面:
https://docs.angularjs.org/error/$injector/unpr?p0=configProvider%20%3C-%20config
您似乎没有重新定义模块或将控制器注入控制器。
我认为您出现此错误的最可能原因(基于假设没有其他问题以及我遇到类似问题的事实)是您要么不包含此错误在您的karma.conf.js文件中配置,或者如果您这样做,则在测试后将其包含在内。文件的顺序在Karma中很重要:
http://karma-runner.github.io/0.8/config/files.html
你现在可能已经找到了你的问题,但我有一个类似的问题,并且在寻找它时会得到答案。