使用Angular.JS中的$ httpBackend测试需要$ resource的服务

时间:2014-03-13 19:55:04

标签: javascript angularjs

我已经在SO上查看了几个答案,并阅读了官方文档。从我所见,这应该有效。

在我的服务中,我有:

var RESTServices = angular.module('RESTServices', []);

RESTServices.service('restCall', ['$resource', function($resource){
    return {
        servList: function(args, scope){
            // Lists servers from the API method
            $resource('http://localhost:1000/apiv1/servers')
                .get(args).$promise.then(function (result) {
                    scope.servers = result.server_list;
                    }, function(error){
                    errorHandler(scope, error)
                })
            }, ... etc.,
    };
}]);

我正在尝试运行测试:

describe('Services that require API calls', function() {
    var restCall,
        $httpBackend;
    beforeEach(function () {
        module('RESTServices');

        inject(function (_$httpBackend_, _restCall_) {
            restCall = _restCall_;
            $httpBackend = _$httpBackend_;
        });
    });
    it('Should supply a list of servers', function () {
        //var serverList = ['Thufir Hawat', 'Duncan Idaho'];
        //$httpBackend.expectGET('http://localhost:1000/apiv1/servers')
        //    .respond({server_list: serverList});
        // Would continue writing if $resource could inject...
    });
});

但是,当我这样做时,我会收到以下消息:

Chrome 31.0.1650 (Windows Vista) Services that require API calls Should supply a list of servers FAILED
        Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- restCall
        http://errors.angularjs.org/1.2.11/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-%20restCall
            at E:/workspace/JSUI/app/lib/angular/angular.js:78:12
            at E:/workspace/JSUI/app/lib/angular/angular.js:3543:19
            at Object.getService [as get] (E:/workspace/JSUI/app/lib/angular/angular.js:3670:39)
            at E:/workspace/JSUI/app/lib/angular/angular.js:3548:45
            at getService (E:/workspace/JSUI/app/lib/angular/angular.js:3670:39)
            at invoke (E:/workspace/JSUI/app/lib/angular/angular.js:3697:13)
            at Object.instantiate (E:/workspace/JSUI/app/lib/angular/angular.js:3718:23)
            at Object.<anonymous> (E:/workspace/JSUI/app/lib/angular/angular.js:3586:24)
            at Object.invoke (E:/workspace/JSUI/app/lib/angular/angular.js:3707:17)
            at E:/workspace/JSUI/app/lib/angular/angular.js:3549:37
        Error: Declaration Location
            at window.inject.angular.mock.inject (E:/workspace/JSUI/app/lib/angular/angular-mocks.js:2134:25)
            at null.<anonymous> (E:/workspace/JSUI/test/unit/servicesSpec.js:133:9)
Chrome 31.0.1650 (Windows Vista): Executed 30 of 30 (1 FAILED) (0.497 secs / 0.14 secs)

我读过的所有内容都告诉我这应该有效,所以我错过了什么?可以使用$ httpBackend来模拟$ resource,对吗?谷歌小组的帖子(包括this one)似乎表明它应该没有问题。

1 个答案:

答案 0 :(得分:5)

在尝试了很多事情并做了更多的研究后,似乎是包含“&nbsp;资源”声明的模块。需要包含在测试中才能工作。例如,我宣布了&#39; ngResource&#39;在模块myApp中的app.js中,所以一旦我包含它,测试就可以了。

app.js中的

var myApp = angular.module('myApp', [
  'ngRoute',
  'ngCookies',
  'ngResource',
  'myControllers',
  'myDirectives',
  'myServices',
  'RESTServices'
]);
规范中的

describe('Testing Services that require API calls', function() {

    var restCall,
        $httpBackend,
        $resource,
        scope;

    beforeEach(function () {

        module('myApp'); //this line fixed it
        module('RESTServices');

        inject(function (_$httpBackend_, _restCall_, _$resource_) {
            $httpBackend = _$httpBackend_;
            $resource = _$resource_;
            restCall = _restCall_;
        });

        scope = {};

    });

    describe('servList', function() {
        it('Should supply a list of servers', function () {
            var serverList = ['Thufir Hawat', 'Duncan Idaho'];
            $httpBackend.expectGET('http://localhost:1000/apiv1/servers')
                .respond({server_list: serverList});
            restCall.servList({}, scope);
            $httpBackend.flush();
            expect(arraysEqual(scope.servers, serverList)).toBeTruthy();
        });
    });
});

ETA:有人说他们通过在我添加module('ngResource');的地方添加module('RESTServices');来解决这个问题。我愿意打赌,重要的是将ngResource直接或间接导入您的测试中。