我看了几个关于 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,对吗?来自 google 组的帖子(包括 this one 似乎表明它应该可以正常工作。
最佳答案
在尝试了许多事情并进行了更多研究之后,似乎需要将包含“ngResource”声明的模块包含在测试中才能使其正常工作。例如,我在 myApp 模块的 app.js 中声明了“ngResource”,所以一旦我包含它,测试就会工作。
在 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();
});
});
});
预计到达时间:有人附和说他们通过添加 module('ngResource');
解决了这个问题,我添加了 module('RESTServices');
。我敢打赌,重要的是将 ngResource 直接或间接导入到您的测试中。
关于javascript - 在 Angular.JS 中使用 $httpBackend 测试需要 $resource 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389500/