$ httpBackend未定义,但包含angular-mock [英] $httpBackend is undefined although angular-mock is included

查看:86
本文介绍了$ httpBackend未定义,但包含angular-mock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的测试。我收到 $ httpBackend 未定义的错误。

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个规格,1个失败
规格列表|失败
对象服务应该调用正确的API地址来获取所有对象
错误:[$ injector:unpr]未知提供者:configProvider< - config
http://errors.angularjs.org/1.3.9/ $ injector / unpr?p0 = configProvider%20%3C-%20config
错误:[$ injector:unpr]未知提供者: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 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 )在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
错误:声明位置
at 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
at jasmineInterface.describe(< a href =http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2969:18 =nofollow> http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2969:18 )
http:// localhost / WebRenter / Test / objects / repository.objects.Spec.js:4:1
TypeError:无法读取未定义的属性'whenGET'
TypeError:无法在Object处读取未定义
的属性'whenGET'。 ( http:// localhost / WebRenter / Test / objects / repository。 objects.Spec.js:20:20 )在attemptSync上的
http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1741:24
在QueueRunner.run( http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1729:9
在QueueRunner.execute(< a href =http://localhost/WebRenter/Scripts/jasmine/jasmine.js:1714:10 =nofollow> 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
at Object.fn(< a href =http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2059:43 =nofollow> http://localhost/WebRenter/Scripts/jasmine/jasmine.js:2059:43 )来自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

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)

这是datacontext:

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

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

这是objects.repository文件的开头:

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

这是配置:

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

})();


推荐答案

根据您提供的信息,首先,查看您的第一条错误消息链接到的页面:

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.

我认为你有这个错误的最可能的原因(基于假设没有其他问题,以及我遇到类似问题的事实)是你在karma.conf.js文件中没有包含配置,或者如果你这样做,你在考试后加入了它。文件的顺序在Karma中很重要:

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.

这篇关于$ httpBackend未定义,但包含angular-mock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆