我正在为我的Listservice编写一个jasmine测试(单元测试)。但是当我在该服务中调用getList方法时,它显示Undefined而不是baseUrl。这个Listservice依赖于另一个服务,即Catalogservice。我想,我在这里做错了什么。帮助我
这是错误
Error: Unexpected request: GET undefined/patients
Service.js:
workerServices.factory('ListService',
['$resource','Restangular','$q','$rootScope','$filter','$location','$state',
function($resource, Restangular, $q, $rootScope, $filter,$location,$state) {
return {
getList: function(baseUrl, resource, searchString,query) {
var listItems = $q.defer();
Restangular.setBaseUrl($rootScope.baseUrl);
var queryString = '';
if (searchString == 'undefined' || searchString == null)
{
queryString = resource;
}
else{
queryString = resource + '?' + searchString;
}
Restangular.all(queryString).getList().then(function(data) {
if (query == 'undefined' || query == null ){
listItems.resolve(data);
}
},
function(response){
if(response.status != 200){
alert("Server is down.Please try after some time");
listItems.reject();
}
});
return listItems.promise;
}
}]);
ServiceSpec.js:
describe('Services', function() {
beforeEach(angular.mock.module('myApp'));
beforeEach(angular.mock.module('workerServices'));
var scope, service, httpBackend,Restangular,promise1,promise,q;
beforeEach(angular.mock.inject(function($httpBackend, $rootScope, ListService,_Restangular_,$q) {
httpBackend = $httpBackend;
scope = $rootScope.$new();
Restangular = _Restangular_;
service = ListService;
q = $q;
}));
it('reading Listservice data from ListViewController', function() {
httpBackend.expectGET('/././rest').respond('[{"id":258,"title":"Mr","firstName":"Johnson"}]');
promise = service.getList('', 'patients', null, null);
scope.$digest();
httpBackend.flush();
console.log(scope.resource['patients'].id);
});
});
Thank you.