javascript - Mocha and Chai test fails when testing function with setInterval - Stack Overflow

I'm new to TDD and working with Mocha and Chai. I have created a test that passes when a value is

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.

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.

Share Improve this question edited Mar 3, 2016 at 19:05 MikeBird asked Mar 3, 2016 at 17:13 MikeBirdMikeBird 3961 gold badge5 silver badges17 bronze badges 2
  • With the code above it's not clear how the code should work. Is increases a function from an assertion library? where thing is defined? – thitemple Commented Mar 3, 2016 at 18:23
  • increases is from the Chai assertion library. Thing is defined in my javascript – MikeBird Commented Mar 3, 2016 at 19:06
Add a ment  | 

1 Answer 1

Reset to default 10

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744124677a4559563.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信