如何为 运行 块中的间隔编写 angularjs 单元测试
How to write angularjs unit test for interval which is in a run block
我正在为 $interval 编写单元测试。代码是这样的:
angular.module('serviceInvoker', []).
run(function($rootScope, $http, $interval, DateTimeFormatter) {
$rootScope.loadData = function(serviceName, dataName) {
$http.get(serviceName)
.then(function(result) {
serviceSuccessFunc(result, dataName)
})
.catch(function(error) {
serviceErrorFunc(error, dataName)
});
$rootScope.current = moment().toISOString();
$rootScope.now = moment($rootScope.current).format(DateTimeFormatter.DayHoursFormat);
};
$rootScope.autoRefresh = function(serviceName, dataName, interval) {
$interval(function() {
$rootScope.loadData(serviceName, dataName)
}, interval);
};
var serviceSuccessFunc = function(result, dataName) {
$rootScope[dataName] = result.data;
};
var serviceErrorFunc = function(error, dataName) {
$rootScope[dataName] = error;
};
});
测试代码是这样的:
describe('serviceInvoker', function() {
beforeEach(module('serviceInvoker'));
beforeEach(module(function($provide) {
$provide.value('DateTimeFormatter', {
DayHoursFormat: 'HH:mm'
});
$provide.value('serviceSuccessFunc', jasmine.createSpy());
$provide.value('serviceErrorFunc', jasmine.createSpy());
}));
var $interval;
beforeEach(inject(function (_$rootScope_, _$interval_, _serviceSuccessFunc_, _serviceErrorFunc_) {
$rootScope = _$rootScope_;
scope = $rootScope.$new();
$interval = _$interval_;
serviceSuccessFunc = _serviceSuccessFunc_;
serviceErrorFunc = _serviceErrorFunc_;
}));
describe("loadData function ", function () {
it("should expose loadData function to $rootScope", function () {
expect(angular.isFunction($rootScope.loadData)).toBe(true);
});
it("should be called", inject(function($http) {
spyOn($rootScope, 'loadData');
$rootScope.loadData('service', 'data');
expect($rootScope.loadData).toHaveBeenCalledWith('service', 'data');
}));
});
describe("autoRefresh function ", function () {
it("should expose autoRefresh function to $rootScope", function () {
expect(angular.isFunction($rootScope.autoRefresh)).toBe(true);
});
it("should be called", function() {
var $intervalspy = jasmine.createSpy('$interval', $interval);
spyOn($rootScope, 'autoRefresh').and.callThrough();
$rootScope.autoRefresh('service', 'data','interval');
expect($rootScope.autoRefresh).toHaveBeenCalledWith('service', 'data', 'interval');
expect($intervalspy).toHaveBeenCalled();
// expect($intervalspy).toHaveBeenCalledWith(jasmine.any(Function), 1000);
});
});
});
但是 interval 有一个错误:Error: Expected spy $interval to have been called.
我不知道如何为函数内部的间隔编写单元测试(在 运行 块中),谁能帮我一些忙?
$intervalspy
除了测试本身,没有在任何地方使用。它没有被调用。
与任何其他经过测试的服务一样,$interval
应该被监视、模拟或存根。它可以用像 这样的装饰器来监视,存根更简单:
beforeEach(() => {
module({ $interval: jasmine.createSpy() })
});
...
$rootScope.autoRefresh('service', 'data', 'interval');
expect($interval).toHaveBeenCalledWith(jasmine.any(Function), 'interval');
expect($rootScope.loadData).not.toHaveBeenCalled(),
var intervalCallback = $interval.calls.first().args[0];
intervalCallback();
expect($rootScope.loadData).toHaveBeenCalledWith(...),
我正在为 $interval 编写单元测试。代码是这样的: angular.module('serviceInvoker', []).
run(function($rootScope, $http, $interval, DateTimeFormatter) {
$rootScope.loadData = function(serviceName, dataName) {
$http.get(serviceName)
.then(function(result) {
serviceSuccessFunc(result, dataName)
})
.catch(function(error) {
serviceErrorFunc(error, dataName)
});
$rootScope.current = moment().toISOString();
$rootScope.now = moment($rootScope.current).format(DateTimeFormatter.DayHoursFormat);
};
$rootScope.autoRefresh = function(serviceName, dataName, interval) {
$interval(function() {
$rootScope.loadData(serviceName, dataName)
}, interval);
};
var serviceSuccessFunc = function(result, dataName) {
$rootScope[dataName] = result.data;
};
var serviceErrorFunc = function(error, dataName) {
$rootScope[dataName] = error;
};
});
测试代码是这样的:
describe('serviceInvoker', function() {
beforeEach(module('serviceInvoker'));
beforeEach(module(function($provide) {
$provide.value('DateTimeFormatter', {
DayHoursFormat: 'HH:mm'
});
$provide.value('serviceSuccessFunc', jasmine.createSpy());
$provide.value('serviceErrorFunc', jasmine.createSpy());
}));
var $interval;
beforeEach(inject(function (_$rootScope_, _$interval_, _serviceSuccessFunc_, _serviceErrorFunc_) {
$rootScope = _$rootScope_;
scope = $rootScope.$new();
$interval = _$interval_;
serviceSuccessFunc = _serviceSuccessFunc_;
serviceErrorFunc = _serviceErrorFunc_;
}));
describe("loadData function ", function () {
it("should expose loadData function to $rootScope", function () {
expect(angular.isFunction($rootScope.loadData)).toBe(true);
});
it("should be called", inject(function($http) {
spyOn($rootScope, 'loadData');
$rootScope.loadData('service', 'data');
expect($rootScope.loadData).toHaveBeenCalledWith('service', 'data');
}));
});
describe("autoRefresh function ", function () {
it("should expose autoRefresh function to $rootScope", function () {
expect(angular.isFunction($rootScope.autoRefresh)).toBe(true);
});
it("should be called", function() {
var $intervalspy = jasmine.createSpy('$interval', $interval);
spyOn($rootScope, 'autoRefresh').and.callThrough();
$rootScope.autoRefresh('service', 'data','interval');
expect($rootScope.autoRefresh).toHaveBeenCalledWith('service', 'data', 'interval');
expect($intervalspy).toHaveBeenCalled();
// expect($intervalspy).toHaveBeenCalledWith(jasmine.any(Function), 1000);
});
});
});
但是 interval 有一个错误:Error: Expected spy $interval to have been called.
我不知道如何为函数内部的间隔编写单元测试(在 运行 块中),谁能帮我一些忙?
$intervalspy
除了测试本身,没有在任何地方使用。它没有被调用。
与任何其他经过测试的服务一样,$interval
应该被监视、模拟或存根。它可以用像
beforeEach(() => {
module({ $interval: jasmine.createSpy() })
});
...
$rootScope.autoRefresh('service', 'data', 'interval');
expect($interval).toHaveBeenCalledWith(jasmine.any(Function), 'interval');
expect($rootScope.loadData).not.toHaveBeenCalled(),
var intervalCallback = $interval.calls.first().args[0];
intervalCallback();
expect($rootScope.loadData).toHaveBeenCalledWith(...),