如何用Sinon测试Ember.run.later? [英] How do I test Ember.run.later with Sinon?

查看:138
本文介绍了如何用Sinon测试Ember.run.later?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var MyObject = Ember.Object.extend({
setFooToBarLater:function(){
Ember.run.later(this,'set','foo','bar',500);
}
});

我想使用Sinon的假时钟进行测试。这是我试过的:

  var clock = sinon.useFakeTimers(); 
var myObject = MyObject.create();
myObject.setFooToBarLater();
clock.tick(600);
expect(myObject.get('foo'))。to.be('bar');

但是期望总是在设置。我也尝试在运行循环中包装 clock.tick

  Ember.run(钟,'tick',600); 


解决方案

请注意,sinon.useFakeTimers(默认情况下)覆盖窗口。日期。这已经成为一个问题,一旦你已经安装了一个安排的Ember的runloop和useFakeTimers()被打开。这是因为Backburner(Ember.js runloop实现)setTimeout /核心调度方法/尝试很难优化调用window.setTimeout(在ember.js中查找 executeAt



经过sinon来源后,我做了以下mod,让这两个人很好地相处( gist )。它指示sinon不要触摸窗口。日期和补丁sinon.test()等待几个更多的ticks后测试机构,允许计划的计时器获得执行和异步代码

  sinon._originalUseFakeTimers = sinon.useFakeTimers; 
sinon.useFakeTimers = function(){
//确保我们不覆盖window.Date用于
// Backburner.setTimeout()来优化window.setTimeout()调用数
return sinon._originalUseFakeTimers.apply(this,[new Date()。getTime(),setTimeout,setInterval,clearTimeout,clearInterval]);
};

sinon._originalTest = sinon.test;
sinon.test = function(callback){
return sinon._originalTest.call(this,function(){
callback.apply(this,arguments);

//等待进一步的runloops完成(2s是mocha超时)
this.clock.tick(2000);
});
};


I have some Ember code that sets a timeout:

var MyObject = Ember.Object.extend({
  setFooToBarLater: function() {
    Ember.run.later(this, 'set', 'foo', 'bar', 500);
  }
});

I'd like to test that using Sinon's fake clock. Here's what I tried:

var clock = sinon.useFakeTimers();
var myObject = MyObject.create();
myObject.setFooToBarLater();
clock.tick(600);
expect(myObject.get('foo')).to.be('bar');

But the expect always runs before the set. I also tried wrapping the clock.tick in a run-loop:

Ember.run(clock, 'tick', 600);

解决方案

Beware that sinon.useFakeTimers (by default on) overrides the window.Date. That becomes an issue once you already have a scheduled Ember runloop and the useFakeTimers() is turned on. This is because Backburner (the Ember.js runloop implementation) setTimeout /core scheduling method/ tries hard to optimize calling window.setTimeout (look for executeAt in ember.js)

After going through the sinon source I made the following mod to let those two get along nicely (gist). It instructs sinon not to touch the window.Date and also patches sinon.test() to wait few more ticks after the test body which allows scheduled timers to get executed and async codes

sinon._originalUseFakeTimers = sinon.useFakeTimers;
sinon.useFakeTimers = function() {
    // make sure we don't override window.Date used in
    // Backburner.setTimeout() to optimize window.setTimeout() call numbers
    return sinon._originalUseFakeTimers.apply(this, [new Date().getTime(), "setTimeout", "setInterval", "clearTimeout", "clearInterval"]);
};

sinon._originalTest = sinon.test;
sinon.test = function(callback) {
    return sinon._originalTest.call(this, function() {
        callback.apply(this, arguments);

        // wait for further runloops to finish (2s is the mocha timeout)
        this.clock.tick(2000);
    });
};

这篇关于如何用Sinon测试Ember.run.later?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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