Vlado Pandžić
Vlado Pandžić

Reputation: 5058

$httpBackend is undefined although angular-mock is included

This is my test. I get error that $httpBackend is undefined.

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 specs, 1 failure Spec List | Failures Objects Service should call right API adress to get all objects Error: [$injector:unpr] Unknown provider: configProvider <- config http://errors.angularjs.org/1.3.9/$injector/unpr?p0=configProvider%20%3C-%20config Error: [$injector:unpr] Unknown provider: configProvider <- config http://errors.angularjs.org/1.3.9/$injector/unpr?p0=configProvider%20%3C-%20config at http://localhost/WebRenter/Scripts/vendor/angular.js:64:20 at http://localhost/WebRenter/Scripts/vendor/angular.js:3995:21 at Object.getService [as get] (http://localhost/WebRenter/Scripts/vendor/angular.js:4142:53) at 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) at QueueRunner.run (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1729:9) at QueueRunner.execute (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10) Error: Declaration Location at window.inject.angular.mock.inject (http://localhost/WebRenter/bower_components/angular-mocks/angular-mocks.js:2407:25) at Suite. (http://localhost/WebRenter/Test/objects/repository.objects.Spec.js:9:16) at addSpecsToSuite (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:725:25) at Env.describe (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:695:7) at jasmineInterface.describe (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2969:18) at http://localhost/WebRenter/Test/objects/repository.objects.Spec.js:4:1 TypeError: Cannot read property 'whenGET' of undefined TypeError: Cannot read property 'whenGET' of undefined at Object. (http://localhost/WebRenter/Test/objects/repository.objects.Spec.js:20:20) at attemptSync (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1741:24) at QueueRunner.run (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1729:9) at QueueRunner.execute (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10) at Spec.Env.queueRunnerFactory (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:608:35) at Spec.execute (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:346:10) at Object.fn (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2059:43) at attemptAsync (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1771:24) at QueueRunner.run (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1726:9) at QueueRunner.execute (http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10)

This is 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;
            }///

        });
        //
    }); /**/
 }
} ]);

This is start of objects.repository file:

(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;
    }

This is config:

(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);
    }
} ]);

})();

Upvotes: 1

Views: 2779

Answers (1)

Aaron Snook
Aaron Snook

Reputation: 31

Based on the information you've provided, first, look at the page that your first error message links to:

https://docs.angularjs.org/error/$injector/unpr?p0=configProvider%20%3C-%20config

It doesn't look like you're redefining your module or injecting a controller into a controller.

I think the most likely reason you're having this error (based on assuming there aren't other problems, and the fact that I had a similar problem) is that you either didn't include config in your karma.conf.js file, or if you did, you included it after your test. The order of files matters in Karma:

http://karma-runner.github.io/0.8/config/files.html

You've probably figured out your problem by now, but I had a similar one and would've appreciated an answer when looking for it.

Upvotes: 0

Related Questions