在PhantomJS中使用Jasmine测试$ interval [英] Testing a $interval with Jasmine in PhantomJS

查看:89
本文介绍了在PhantomJS中使用Jasmine测试$ interval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我的间隔永远不会被触发。

It appears that my interval is never triggered.

我有一个包含<$ c的指令$ c> $ interval 我想测试它。我删除了所有与指令相关的代码,并将其添加到其控制器中:

I have a directive which contains a $interval and I want to test it. I've removed all the directive-related code and added this piece instead in its controller:

window.called = 0;
window.interval = $interval(function () {
    window.called++;
    console.log('interval ' + window.called); // 4
}, 10);
console.log('initialized'); // 1

测试如下:

describe('myDirective', function () {
    beforeEach(module('myModule'));
    beforeEach(function($compile, $rootScope) {
        /* ... compile element in its own scope ... */
    });
    it('should run the interval', function () {
        console.log(window.interval); // 2
        waitsFor(function () {
            console.log('tick'); // 3
            return false;
        }, 1000);
    });
});

这是一个愚蠢的测试。出于调试目的, waitsFor 方法实际上一直返回false。但这就是我在控制台中看到的全部内容:

This is a dumb test. The waitsFor method actually returns false all the time, for debugging purposes. But this is all I see in the console:

initialized // 1
Object: {then: ..} // 2
tick // 3
tick // 3
tick // 3
tick // 3
..

并最终导致测试失败。我从未在日志中看到一个 interval 。我的代码总体上有什么问题,或者我缺少Jasmine / PhantomJS特有的东西吗?

and eventually the test failure. I never see a single interval in the logs. Is there something wrong with my code in general or is there something particular to Jasmine/PhantomJS that I'm missing?

推荐答案

$ interval在角度模拟中有一个模拟实现。
确保使用的是与角度版本匹配的角度模拟版本。

$interval has a mock implementation in angular-mocks. Make sure you are using a version of angular-mocks that matches your angular version.

$ interval的模拟版本有一个用于控制滴答的flush方法。
请参阅 ngMock。$ interval

The mock version of $interval has a flush method for controlling ticks. See ngMock.$interval

通过演示查看此小提琴

//--- CODE --------------------------
angular.module('myModule', []).service('myModuleService', ['$interval', function ($interval) {
    var called = 0;
    $interval(function () {
        called++;
    }, 10);
    this.getCalled = function () {
        return called;
    }
}]);

// --- SPECS -------------------------

describe('test $interval', function () {

    it('calls the interval callback', function () {
        var service, $interval;
        angular.mock.module('myModule');
        angular.mock.inject(function (myModuleService, _$interval_) {
            // Initialize the service under test instance
            service = myModuleService;
            $interval = _$interval_;
        });
        expect(service.getCalled()).toEqual(0);
        $interval.flush(11);
        expect(service.getCalled()).toEqual(1);
        $interval.flush(10);
        expect(service.getCalled()).toEqual(2);
    });
});

这篇关于在PhantomJS中使用Jasmine测试$ interval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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