测试需要使用$ httpBackend在Angular.JS $资源的服务 [英] Testing a service that requires $resource using $httpBackend in Angular.JS
问题描述
我看了看有这么几种答案,并通过官方的文档已经阅读。从我所看到的,这应该是工作。
I've looked at several answers on SO, and have read through the official docs. From what I've seen, this should be working.
在我的服务,我有:
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.,
};
}]);
我试图运行测试:
I am trying to run the test:
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...
});
});
不过,我收到以下消息时,我做的:
However I receive the following message when I do:
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模拟$资源,是否正确?从谷歌组文章(包括这个似乎表明,它应该没有问题的工作
Everything I've read tells me this should be working, so what am I missing? It IS possible to use $httpBackend to mock $resource, correct? Posts from the google group (including this one seem to suggest that it should work without a problem.
推荐答案
尝试很多事情,做一些调查研究后,它似乎是需要被包括在测试中,包括ngResource的声明模块为它工作。例如,我宣布'ngResource在模块对myApp app.js,所以一旦我有这个,测试工作。
After trying many things, and doing some more research, it seems to be that the module that includes the declaration of 'ngResource' needs to be included in the test for it to work. For example, I declared 'ngResource' in app.js in the module myApp, so once I include this, the tests work.
在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();
});
});
});
这篇关于测试需要使用$ httpBackend在Angular.JS $资源的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!