测试:如何在两个序列承诺/ run.laters之间断言?如何跳过`run.later`等待测试? [英] Testing: how to assert between two sequential promises/run.laters? How to skip `run.later` waiting in tests?

查看:87
本文介绍了测试:如何在两个序列承诺/ run.laters之间断言?如何跳过`run.later`等待测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的组件:

App.FooBarComponent = Ember.Component.extend({   
  tagName: "button",   
  status: "Ready",
  revertStatusPeriodMs: 2000,

  click: function() {
    this.set('status', 'Pending');

    // A fake ajax
    this.run()
      .then( function() {
        this.updateStatus('Finished');
      }.bind(this))
      .catch( function() {
        this.updateStatus('Error');
      }.bind(this));
  },

  run: function() {
    return new Ember.RSVP.Promise( function(resolve) {
      Ember.run.later( function() {
        resolve();
      }, 500);
    });
  },

  updateStatus: function(statusText) {
    this.set('status', statusText); 

    var periodMs = this.get('revertStatusPeriodMs') || 1000;

    Ember.run.later( function() {
      this.set('status', 'Ready');
    }.bind(this), periodMs);
  }
});

它做一个简单的事情:点击时,显示一些文本。后来用另一个替换文本。即使以后,它将文本恢复到初始文本。

It does a simple thing: when clicked, it displays some text. Later replaces the text with another one. Even later, it reverts the text to the initial one.

代码工作正常。我的问题是我无法为其编写一个测试。

The code works fine. My problem is that i'm unable to write a test for it.

test('clicking the button should set the label', function(assert) {
  expect(4);

  visit('/');

  assert.equal( find('button').text().trim(), 'Ready', 'Should initially be "Ready"');

  andThen(function() {
    click('button');
    assert.equal( find('button').text().trim(), 'Pending', 'Should "Pending" right after click');
  });

  // This one fires too late!
  andThen(function() {
    assert.equal( find('button').text().trim(), 'Finished', 'Should become "Finished" after promise fulfills');
  });

  andThen(function() {
    assert.equal( find('button').text().trim(), 'Ready', 'Should eventually return to the "Ready" state');
  });
});

我有两个问题:


  1. 我无法测试已完成状态。似乎 andThen 等待所有的承诺和 run.later 完成,而我想测试一个中间状态。我如何在两个顺序承诺之间进行断言/ run.later s?

  1. I'm unable to test the Finished state. It seems that andThen waits for all promises and run.laters to finish, while i want to test an intermediate state. How do i run an assertion between two sequential promises/run.laters?

长时间,测试可以永远完成。我通常不介意重构代码以获得更好的可测试性,但是我拒绝根据环境调整应用程序的时间(例如2000 / dev / prod,0用于测试)。相反,我想使用定时器模拟或其他解决方案。

Times can be long, and tests can take forever to complete. I generally don't mind refactoring the code for better testability, but i refuse to adjust times in the app based on the environment (e. g. 2000 for dev/prod, 0 for test). Instead, i would like to use a timer mock or some other solution.

我尝试过Sinon并失败:当我嘲笑定时器时,从不返回。 这些解决方案都没有帮助我。

I've tried Sinon and failed: when i mock the timer, andThen never returns. Neither of these solutions helped me.

JSBin: http://emberjs.jsbin.com/fohava/2/edit?html,js,output (包含Sinon)

JSBin: http://emberjs.jsbin.com/fohava/2/edit?html,js,output (Sinon is included)

推荐答案

做了一些代码,我基本上有它工作,但它似乎真的很好奇,而不是你想要做的事情。

I did some code where I basically got it working but it seems really finicky and not something you would want to be do be doing..

我这样做了几天前,但从来没有回应,因为我不是快乐 - 它不会给你足够的控制..

I did this a few days ago but never replied because I wasn't happy with it - it doesn't give you enough control..

我现在回复的原因是因为我找到了可能会使用的公关给你: PR 10463

The reason I'm replying now however is because I've found a PR that may be of use to you: PR 10463

我没有看到太多的细节,但是没有看到tly it允许自定义异步测试帮助者通过返回承诺暂停执行

I didn't look into too much detail but apparently it "allows custom async test helpers to pause execution by returning a promise"

我不知道您使用的是什么版本的Ember,但如果您可以持有出来为这个公关,它可以帮助你..

I'm not sure what version of Ember you're using - but if you can hold out for this PR it may help you..

这篇关于测试:如何在两个序列承诺/ run.laters之间断言?如何跳过`run.later`等待测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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