我有一些地方可以使用$ timeout或$ interval延迟在UI中进行操作。这是一个简化的示例:

控制器代码:

$timeout(function() {
    $scope.showElement = true;
}, 10000);


HTML:

<div id="myElement" ng-show="showElement"></div>


我希望能够创建一个端到端量角器测试,以测试#myElement是否在等待10秒后显示。我发现执行此操作的唯一方法是调用browser.sleep(10000),这会导致测试实际延迟10秒。这行得通,但是这些停顿加起来加起来并显着增加了我的测试时间。想象一下您要测试闲置30分钟后是否弹出模态的情况。

有没有办法模拟特定时间的流逝,类似于茉莉花测试中的$ timeout.flush()?

最佳答案

您可以装饰$timeout$interval以覆盖提供给它们的延迟:

Lower-wait-time.js

exports.module = function() {
    angular.module('lowerWaitTimeDecorator', [])
    .config(function($provide) {
        $provide.decorator('$timeout', function($delegate) {
            return function() {
                // The second argument is the delay in ms
                arguments[1] = arguments[1] / 10;
                return $delegate.apply(this, arguments);
            };
        });
    })
};


用法

beforeAll(function() {
    var lowerWaitTime = require('lower-wait-time');
    browser.addMockModule('lowerWaitTimeDecorator', lowerWaitTime.module);
});

afterAll(function() {
    browser.removeMockModule('lowerWaitTimeDecorator');
});

it('My-sped-up-test', function() {

});

10-05 20:48
查看更多