【发布时间】:2014-09-24 11:43:45
【问题描述】:
下面我有 3 个功能完全相同。每个人使用不同的调用方式setTimeout,delay1() 直接使用setTimeout,delay2() 使用angularjs $timeout 和delay3() 使用lodash debounce。它们都工作正常。
当我使用 Jasmine 进行测试时出现问题。 setTimeout 与 jasmine.clock().tick() 方法可以正常工作,但 $timeout 和 debounce 不能
我有兴趣与 Jasmine 一起使用 debounce。我知道我可以将 $timeout.flush() 与 angularjs 一起使用,但 $timeout 和 setTimeout 在我将其与传单地图一起使用的代码中的其他地方给我带来了问题。 debounce 与传单很好地配合使用。
我在这里创建了一个 plunker:plnkr,您将在其中看到 $timeout 和去抖测试未通过,而 setTimeout 测试通过。
有没有办法解决这个问题?谢谢
JS
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $timeout) {
$scope.name = 'World';
$scope.delayed1 = function(){
setTimeout(function(){
$scope.name = "Hello world by setTimeout";
},500)
}
$scope.delayed2 = function(){
$timeout(function(){
$scope.name = "Hello world by $timeout";
},500)
}
$scope.delayed3 = function(){
_.debounce(function(){
$scope.name = "Hello world by debounce";
},500)
}
});
规格
describe('Testing a Hello World controller', function() {
var $scope = null;
var ctrl = null;
//you need to indicate your module in a test
beforeEach(module('plunker'));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
ctrl = $controller('MainCtrl', {
$scope: $scope
});
}));
it('should say hallo to the World', function() {
expect($scope.name).toEqual('World');
});
it('should say Hello world by setTimeout', function() {
jasmine.clock().install();
$scope.delayed1();
jasmine.clock().tick(600);
expect($scope.name).toEqual('Hello world by setTimeout');
jasmine.clock().uninstall();
});
it('should say Hello world by timeout', function() {
jasmine.clock().install();
$scope.delayed2();
jasmine.clock().tick(600);
expect($scope.name).toEqual('Hello world by timeout');
jasmine.clock().uninstall();
});
it('should say Hello world by debouce', function() {
jasmine.clock().install();
$scope.delayed3();
jasmine.clock().tick(600);
expect($scope.name).toEqual('Hello world by debouce');
jasmine.clock().uninstall();
});
});
【问题讨论】:
-
对此有什么结论吗?
-
不,还没有。有人吗?
标签: javascript angularjs jasmine leaflet lodash