使用setInterval测试功能时,Mocha和Chai测试失败 [英] Mocha and Chai test fails when testing function with setInterval

查看:97
本文介绍了使用setInterval测试功能时,Mocha和Chai测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是TDD的新手,并与Mocha和Chai一起工作.我创建了一个测试,该测试在值增加时通过,但是当该增加值放在setInterval中时,它将失败.这段代码的目的是在屏幕上移动一些东西.

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

测试:

describe('Thing', function() {

    it('should increase position', function(){
        assert.increases(startMovingThing, thing, 'position');
    });

});

我如何才能通过此考试(或应该通过什么考试)?

我不希望moveThing()在间隔之外,因为如果清除间隔并调用了函数,则该东西不应移动.

解决方案

好吧,问题在于您使用的是异步的setInterval,并且您试图断言该值是以同步方式更改的.

这是测试的修改版本,使用sinonjs模拟经过的时间.

var assert = require('chai').assert;
var sinon = require('sinon');

var thing = { position: 0 }
var thingOnScreen = { style: { left: '' } };

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

describe('Thing', function() {

  beforeEach(function() {
    this.clock = sinon.useFakeTimers();
  });

  afterEach(function() {
    this.clock = sinon.restore();
  });

  it('should increase position', function(){
    startMovingThing();
    this.clock.tick(101);
    assert.equal(thing.position, 10);
  });
});

总而言之,sinonjs取代了setInterval的全局功能,并且无需等待指定的毫秒数即可执行代码.

I'm new to TDD and working with Mocha and Chai. I have created a test that passes when a value is increased, but when that increase is put within a setInterval, it fails. The objective of this code is to have something move across the screen.

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

test:

describe('Thing', function() {

    it('should increase position', function(){
        assert.increases(startMovingThing, thing, 'position');
    });

});

How can I get this test (or what should the test be) to pass?

I don't want moveThing() to be outside of the interval, because if the interval is cleared and the function is called, the thing should not move.

解决方案

Ok, the problem is that you're using setInterval which is async and you're trying to assert that the value was changed in a synchronous way.

Here's a modified version of your test, using sinonjs to simulate that the time passed.

var assert = require('chai').assert;
var sinon = require('sinon');

var thing = { position: 0 }
var thingOnScreen = { style: { left: '' } };

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

describe('Thing', function() {

  beforeEach(function() {
    this.clock = sinon.useFakeTimers();
  });

  afterEach(function() {
    this.clock = sinon.restore();
  });

  it('should increase position', function(){
    startMovingThing();
    this.clock.tick(101);
    assert.equal(thing.position, 10);
  });
});

In summary, sinonjs is replacing the global functionality of setInterval and is executing the code without having to wait for the specified milliseconds.

这篇关于使用setInterval测试功能时,Mocha和Chai测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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