我有我愿意测试的角度服务功能:
doSomeTask: function (ids, callback) {
$http.put('/get_job', {'ids': ids}).success(function (data) {
var statusCheck = $interval(function() {
$http.get('check_status?=' + data.job_id).success(function (data) {
if(data.completed) {
$interval.cancel(statusCheck);
// and do something...
}
});
}, 2000);
});
并对其进行以下测试:
describe('MyService', function() {
var myService;
var $httpBackend;
var $interval;
beforeEach(module('myModule'));
beforeEach(inject(function(_$httpBackend_, _$interval_, $rootScope, MyService) {
$httpBackend = _$httpBackend_;
$interval = _$interval_;
myService = MyService;
}));
describe('doSomeTask', function () {
beforeEach(function () {
$httpBackend.expectPUT("/get_job").respond({ job_id: '123' });
$httpBackend.expectGET("/status_check").respond({ completed: true });
});
it('should do it well', inject(function ($interval) {
var ids = [1,2,3];
myService.doSomeTask(ids);
$httpBackend.flush();
$interval.flush();
}));
});
});
似乎我的间隔永远不会被刷新(第二个期望永远不会得到满足,我在间隔体内的断点从未被触发)。似乎我正在使用模拟(在刷新实现中提出了一些建议)。Angular 和 mocks 的版本是一样的……
我究竟做错了什么?我怎样才能刷新我的间隔?