如何使用第三方组件和回调测试EmberJS?

时间:2014-06-29 14:49:32

标签: javascript testing ember.js

我们正在对我们的应用运行集成测试,并遇到使用Stripe JavaScript库的问题。我们有一个包装库的组件,它在它开始令牌创建过程到它完成组件被销毁的时间之间(这反过来又使我们的其余测试失败了。)典型的“修复”是为了包装该方法在Ember.run中,它似乎没有任何影响。让我举个例子。

actions: {
  update: function() {
    // Starting here this.get('isDestroyed') == false
    Stripe.card.createToken({
      number: "xx",
      cvc: "xx"
    }, function() {
       // Once we are here this.get('isDestroyed') == true
    })
  }
}

因为它早期被摧毁,所以不采取正常行动。在等待回调完成时,我们如何让Ember保持活力?

更新1

我在下面发布了一个答案,但很好奇为什么我不想这样做和/或为什么它不是官方文档的一部分。

更新2

我在下面发布的这个答案修复了测试,但实际网站本身停止工作(只是坐在那里。)所以虽然看起来很容易解决问题,但显然没有完全发挥作用。

1 个答案:

答案 0 :(得分:1)

解决方案

似乎我手动启动和停止runloop它确实有效。 But that's not what the documentation says(尽管以这种方式这样做是完全合理的。)

actions: {
  update: function() {
    Ember.run.begin()
    Stripe.card.createToken({
      number: "xx",
      cvc: "xx"
    }, function() {
       // Perform actions
       Ember.run.end()
    })
  }
}

提倡Ember.run的参考文献 - >

  1. http://balinterdi.com/2014/05/09/ember-dot-run-dot-bind.html
  2. http://emberjs.com/guides/understanding-ember/run-loop/#toc_how-do-i-tell-ember-to-start-a-run-loop
  3. What is Ember RunLoop and how does it work?(具体说明你不应该使用开始/结束,因为这样做的是什么)
  4. https://github.com/eoinkelly/ember-get-what-from-where/blob/master/ember-run-loop.md
  5. http://alexmatchneer.com/blog/2013/01/12/everything-you-never-wanted-to-know-about-the-ember-run-loop/(看起来与堆栈溢出问题的内容大致相同)