我有一个服务,它具有以下方法(以及其他方法),它返回 $http promise
function sessionService($http, serviceRoot) {
return {
getAvailableDates: function () {
return $http.get(serviceRoot + '/session/available_dates');
}
};
};
angular.module('app').service('sessionService', ['$http', 'serviceRoot', sessionService]);
然后是另一个工厂,它有点包装它并将数据缓存/添加到 localStorage。这将返回一个常规 promise
angular.module('app')
.factory('AvailableDates', AvailableDates);
AvailableDates.$inject = ['sessionService', '$window', '$q'];
function AvailableDates(sessionService, $window, $q) {
var availableDates = [];
return {
getAvailableDates: getAvailableDates
};
function getAvailableDates() {
var deferred = $q.defer();
var fromStorage = JSON.parse($window.sessionStorage.getItem('validDates'));
if (availableDates.length > 0) {
deferred.resolve(availableDates);
} else if (fromStorage !== null) {
deferred.resolve(fromStorage);
} else {
sessionService.getAvailableDates()
.success(function (result) {
availableDates = result;
$window.sessionStorage.setItem('validDates', JSON.stringify(availableDates));
deferred.resolve(availableDates);
});
}
return deferred.promise;
}
}
这一切都运行良好。我的问题是我不知道如何在模拟 sessionService 时测试这个东西。我已阅读所有相关的 stackoverflow 问题,并尝试了各种不同的方法,但无济于事。
这是我当前的测试:
describe('testing AvailableDates factory', function () {
var mock, service, rootScope, spy, window, sessionStorageSpy, $q;
var dates = [ "2014-09-27", "2014-09-20", "2014-09-13", "2014-09-06", "2014-08-30" ];
var result;
beforeEach(module('app'));
beforeEach(function() {
return angular.mock.inject(function (_sessionService_, _AvailableDates_, _$rootScope_, _$window_, _$q_) {
mock = _sessionService_;
service = _AvailableDates_;
rootScope = _$rootScope_;
window = _$window_;
$q = _$q_;
});
});
beforeEach(inject(function () {
// my service under test calls this service method
spy = spyOn(mock, 'getAvailableDates').and.callFake(function () {
return {
success: function () {
return [ "2014-09-27", "2014-09-20", "2014-09-13", "2014-09-06", "2014-08-30" ];
},
error: function() {
return "error";
}
};
});
spyOn(window.sessionStorage, "getItem").and.callThrough();
}));
beforeEach(function() {
service.getAvailableDates().then(function(data) {
result = data;
// use done() here??
});
});
it('first call to fetch available dates hits sessionService and returns dates from the service', function () {
rootScope.$apply(); // ??
console.log(result); // this is printing undefined
expect(spy).toHaveBeenCalled(); // this passes
expect(window.sessionStorage.getItem).toHaveBeenCalled(); // this passes
});
});
我尝试了各种方法,但不知道如何测试AvailableDates.getAvailableDates() 调用的结果。当我使用done()时,我收到错误: 超时 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调(我尝试覆盖此值,但没有成功)。
如果我取出 did(),并在调用 .then 后调用 rootScope.$apply(),我会得到一个未定义的值作为结果。
我做错了什么?
最佳答案
我在您的示例中发现了更多问题。
主要问题是模拟中的成功定义。 success是一个函数,它有一个函数作为参数——回调。收到数据时调用回调 - 数据作为第一个参数传递。
return {
success: function (callback) {
callback(dates);
}
};
简化的工作示例在这里 http://plnkr.co/edit/Tj2TZDWPkzjYhsuSM0u3?p=preview
在此示例中,模拟通过模块函数(来自 ngMock)传递给提供者 - 您可以使用键(服务名称)和值(实现)传递对象。该实现将用于注入(inject)。
module({
sessionService:sessionServiceMock
});
我认为测试逻辑应该在一个函数(test)中,将其拆分为 beforeEach 和 test 不是一个好的解决方案。测试是我的例子;它更具可读性,并且具有清晰分离的部分 - 安排、行动、断言。
inject(function (AvailableDates) {
AvailableDates.getAvailableDates().then(function(data) {
expect(data).toEqual(dates);
done();
});
rootScope.$apply(); // promises are resolved/dispatched only on next $digest cycle
expect(sessionServiceMock.getAvailableDates).toHaveBeenCalled();
expect(window.sessionStorage.getItem).toHaveBeenCalled();
});
关于javascript - AngularJS : testing a factory that returns a promise, 在模拟使用 $http 的服务时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31213425/