3

I've been successful writing unit level tests in ember w/ help from the sinon clock but acceptance tests always seem to be an issue.

For example, in my route I intend to wait 5 seconds before I do something

export default Ember.Route.extend({
    model: function() {
        setTimeout(Ember.run.bind(this, function() {
            //after the timeout do something like transition to another route
        }), 5000);
    }
});

In ember testing I'd do a simple visit, assert the currentURL() is good, then do a clock.tick(5001) w/ sinon ... then assert the timer is complete and some state was set/etc.

I realize sinon and the ember run loop don't seem to play nice together but I'm curious what other people are using to test timers like this at the high level (non unit tests /without selenium or sleep hacks).

If run later is required how would you rework the (incorrect) test below to work with clock.tick ?

test("sinon and ember play nice", function(assert) {
    var clock = sinon.useFakeTimers();
    visit("/");
    andThen(function() {
        assert.equal(currentURL(), "/");
    });
    clock.tick(5001);
    andThen(function() {
        assert.equal(currentURL(), "/the-transition-url");
    });
});
4

1 回答 1

3

我认为在所有情况下sinon.useFakeTimers()都会破坏异步和同步助手(在这种情况下是andThen())。

我在类似的情况下测试过类似的东西:

test("sinon and ember play nice", function(assert) {
  const clock = sinon.useFakeTimers();
  visit('/');
  clock.tick(0); // this for simulating rendering right now
  assert.equal(currentURL(), '/');
  clock.tick(5100);
  assert.equal(currentURL(), '/the-transition-url');
  clock.restore();
});

ps:经过大量的异步助手测试,看起来ember-testing框架上的异步实现是基于时间传递的。作为副作用, noandThen()​​被执行,直到clock.tick(0);or clock.tick(50);

ps2:看起来在stackoverflow上还有另一个问题与是否有办法使用假计时器运行Ember.Testing验收测试?还有最新的 ember(2.9 和 2.10),以及 qunit 的升级,当前的代码需要更多的调整

于 2016-05-03T18:15:58.547 回答