【发布时间】:2014-07-11 21:14:02
【问题描述】:
我有以下装饰器,它从$rootScope 中环绕原始$timeout。在控制器内部使用时,它会在作用域被销毁时取消 $timeout 承诺。
angular.module('MyApp').config(['$provide', function ($provide) {
$provide.decorator('$rootScope', ['$delegate', function ($delegate) {
Object.defineProperty($delegate.constructor.prototype, 'timeout', {
value: function (fn, number, invokeApply) {
var $timeout = angular.injector(['ng']).get('$timeout'),
promise;
promise = $timeout(fn, number, invokeApply);
this.$on('$destroy', function () {
$timeout.cancel(promise);
});
},
enumerable: false
});
return $delegate;
}]);
}]);
但是我该如何正确地进行单元测试呢?我有点看到我应该在这里做 2 个测试... 1) 检查是否在调用 $rootScope.timeout() 时调用了原始的 $timeout 以及 2) 检查在销毁范围时是否取消了承诺。
这是我目前的测试套件:
describe('MyApp', function () {
var $rootScope;
beforeEach(function () {
module('MyApp');
inject(['$rootScope', function (_$rootScope_) {
$rootScope = _$rootScope_;
}]);
});
describe('$timeout', function () {
it('<something>', function () {
$rootScope.timeout(function () {}, 2500);
// Test if the real $timeout was called with above parameters
$rootScope.$destroy();
// Test if the $timeout promise was destroyed
});
});
});
这样做的唯一一件事就是给我 100% 的覆盖率。但这不是我想要的……我该如何正确测试呢?
【问题讨论】:
标签: angularjs unit-testing karma-jasmine angular-mock angular-decorator