单元测试角服务使用$暂停时以茉莉花的模拟时钟 [英] Unit Testing Angular Service that uses $timeout with Jasmine's Mock Clock

查看:100
本文介绍了单元测试角服务使用$暂停时以茉莉花的模拟时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有我的,我想是在固定时间间隔重复调用角服务之一的功能。我想做到这一点使用$超时。它看起来是这样的:

I have a function inside one of my angular services that I'd like to be called repeatedly at a regular interval. I'd like to do this using $timeout. It looks something like this:

var interval = 1000; // Or something

var _tick = function () {
     $timeout(function () {
        doStuff();
        _tick();
    }, interval);
};

_tick();

我如何做到这一点 -

我目前就如何进行单元测试难倒这个茉莉?如果我使用 $ timeout.flush()则函数调用无限期发生。如果我用茉莉的模拟时钟, $超时似乎未受影响。基本上,如果我能得到这个工作,我应该是好去:

I'm stumped on how to unit test this with Jasmine at the moment - How do I do this? If I use $timeout.flush() then the function calls occur indefinitely. If I use Jasmine's mock clock, $timeout seems to be unaffected. Basically if I can get this working, I should be good to go:

describe("ANGULAR Manually ticking the Jasmine Mock Clock", function() {
    var timerCallback, $timeout;

    beforeEach(inject(function($injector) {
        $timeout = $injector.get('$timeout');
        timerCallback = jasmine.createSpy('timerCallback');
        jasmine.Clock.useMock();
    }));

    it("causes a timeout to be called synchronously", function() {
        $timeout(function() {
            timerCallback();
        }, 100);
        expect(timerCallback).not.toHaveBeenCalled();
        jasmine.Clock.tick(101);
        expect(timerCallback).toHaveBeenCalled();
    });
});

这两个变化的工作,但不帮我:

These two variations work, but do not help me:

describe("Manually ticking the Jasmine Mock Clock", function() {
    var timerCallback;

    beforeEach(function() {
        timerCallback = jasmine.createSpy('timerCallback');
        jasmine.Clock.useMock();
    });

    it("causes a timeout to be called synchronously", function() {
        setTimeout(function() {
            timerCallback();
        }, 100);
        expect(timerCallback).not.toHaveBeenCalled();
        jasmine.Clock.tick(101);
        expect(timerCallback).toHaveBeenCalled();
    });
});

describe("ANGULAR Manually flushing $timeout", function() {
    var timerCallback, $timeout;

    beforeEach(inject(function($injector) {
        $timeout = $injector.get('$timeout');
        timerCallback = jasmine.createSpy('timerCallback');
    }));

    it("causes a timeout to be called synchronously", function() {
        $timeout(function() {
            timerCallback();
        }, 100);
        expect(timerCallback).not.toHaveBeenCalled();
        $timeout.flush();
        expect(timerCallback).toHaveBeenCalled();
    });
});

在此先感谢!

推荐答案

不要使用茉莉花的时钟让你的测试异步。相反,使用 $ timeout.flush()来保持同步测试的流程。它可能是一个有点棘手的设置,但一旦你得到它那么你的测试将是更快,更容易控制。

Do not make your test Async by using Jasmine's clock. Instead, use $timeout.flush() to synchronously maintain the flow of the test. It may be a bit tricky to setup, but once you get it then your tests will be faster and more controlled.

下面是一个测试使用这种方法吧,做的例子:
<一href=\"https://github.com/angular/angular.js/blob/master/test/ngAnimate/animateSpec.js#L618\">https://github.com/angular/angular.js/blob/master/test/ngAnimate/animateSpec.js#L618

Here's an example of a test that does it using this approach: https://github.com/angular/angular.js/blob/master/test/ngAnimate/animateSpec.js#L618

这篇关于单元测试角服务使用$暂停时以茉莉花的模拟时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆