服务承诺不解决业力
service promise not resolving with karma
我有一个 service factory
可以连接 api
并与之交互以获取数据。这是服务:
angular.module('dbRequest', [])
.factory('Request', ['$http', 'localConfig', function($http, localConfig){
return {
getDataRevision: function(){
return $http({
url: localConfig.dbDataUrl,
method: "GET",
crossDomain: true,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
}
}
}]);
根据 this answer 的提示,这就是我测试该方法的方式:
describe('Service: Request', function () {
var $scope, srvMock, $q, lc, $httpBackend;
beforeEach(module('webApp'));
beforeEach(inject(function($rootScope, Request, _$q_, _$httpBackend_, localConfig){
$scope = $rootScope.$new();
srvMock = Request;
$q = _$q_;
$httpBackend = _$httpBackend_;
lc = localConfig;
$httpBackend.expect('GET', lc.dbDataUrl);
$httpBackend.when('GET', lc.dbDataUrl).respond({
success: ["d1","d2", "d3"]
});
}));
it('should return a promise', function(){
expect(srvMock.getDataRevision().then).toBeDefined();
});
it('should resolve with data', function(){
var data;
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function(res){
data = res.success;
});
srvMock.getDataRevision().then(function(res){
deferred.resolve(res);
});
$scope.$digest();
expect(data).toEqual(["d1","d2", "d3"]);
})
});
should return a promise
通过,但下一个 should resolve with data
失败并出现此错误:
Expected undefined to equal [ 'd1', 'd2', 'd3' ].
然而,service
方法 getDataRevision
被调用,但未被测试中的模拟承诺解析。我该如何更正?
目前您希望模拟数据存在于 data
变量中而不刷新 httpRequest
s,但这不会发生,直到您 flush
所有 httpRequest
秒。 $httpBackend.flush()
所做的是,它 returns 模拟您使用 $httpBackend.when('GET', lc.dbDataUrl).respond
.
所做的特定请求的数据
此外,您不需要创建额外的承诺,这将是一项开销。您可以使用服务函数返回承诺本身,而不是自定义承诺,如下所示。
代码
it('should resolve with data', function(){
var data;
srvMock.getDataRevision().then(function(res){
data = res.success;
});
$scope.$digest();
$httpBackend.flush(); //making sure mocked response has been return
//after .then evaluation only below code will get called.
expect(data).toEqual(["d1","d2", "d3"]);
})
我有一个 service factory
可以连接 api
并与之交互以获取数据。这是服务:
angular.module('dbRequest', [])
.factory('Request', ['$http', 'localConfig', function($http, localConfig){
return {
getDataRevision: function(){
return $http({
url: localConfig.dbDataUrl,
method: "GET",
crossDomain: true,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
}
}
}]);
根据 this answer 的提示,这就是我测试该方法的方式:
describe('Service: Request', function () {
var $scope, srvMock, $q, lc, $httpBackend;
beforeEach(module('webApp'));
beforeEach(inject(function($rootScope, Request, _$q_, _$httpBackend_, localConfig){
$scope = $rootScope.$new();
srvMock = Request;
$q = _$q_;
$httpBackend = _$httpBackend_;
lc = localConfig;
$httpBackend.expect('GET', lc.dbDataUrl);
$httpBackend.when('GET', lc.dbDataUrl).respond({
success: ["d1","d2", "d3"]
});
}));
it('should return a promise', function(){
expect(srvMock.getDataRevision().then).toBeDefined();
});
it('should resolve with data', function(){
var data;
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function(res){
data = res.success;
});
srvMock.getDataRevision().then(function(res){
deferred.resolve(res);
});
$scope.$digest();
expect(data).toEqual(["d1","d2", "d3"]);
})
});
should return a promise
通过,但下一个 should resolve with data
失败并出现此错误:
Expected undefined to equal [ 'd1', 'd2', 'd3' ].
然而,service
方法 getDataRevision
被调用,但未被测试中的模拟承诺解析。我该如何更正?
目前您希望模拟数据存在于 data
变量中而不刷新 httpRequest
s,但这不会发生,直到您 flush
所有 httpRequest
秒。 $httpBackend.flush()
所做的是,它 returns 模拟您使用 $httpBackend.when('GET', lc.dbDataUrl).respond
.
此外,您不需要创建额外的承诺,这将是一项开销。您可以使用服务函数返回承诺本身,而不是自定义承诺,如下所示。
代码
it('should resolve with data', function(){
var data;
srvMock.getDataRevision().then(function(res){
data = res.success;
});
$scope.$digest();
$httpBackend.flush(); //making sure mocked response has been return
//after .then evaluation only below code will get called.
expect(data).toEqual(["d1","d2", "d3"]);
})