延迟测试直到Ember.run.later完成 [英] Delay tests till Ember.run.later is done

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

问题描述

我正在尝试通过 ember addon 进行测试。它工作正常,直到昨天我添加了一些代码,运行后循环使用Em.run.next。

I am trying to get some tests to pass for an ember addon. It was working fine until yesterday I added some code that runs later in the run loops using Em.run.next.

这是我在测试中做什么。

Here is what Im doing in my test.

visit('/').then(function() {
  find('bm-select').click();
  andThen(function() {
    equal(1,1, 'yay');
  });
});

问题是当点击被触发时,稍后的功能在之后执行,然后。当时我所有的测试都完成了,并且会引发错误。我的印象是,等待所有异步的东西完成。

The problem is when click is triggered, the later function is executed after andThen. By that time all my tests are done and it throws error. I am under the impression andThen should wait for all async stuff to finish.

这是我的代码看起来像点击被触发(focusOut事件触发点击)

This is what what my code looks like when click is triggered(focusOut event is triggered on click)

lostFocus: function() {
  if(this.get('isOpen')) {
    Em.run.later(this, function() {
      var focussedElement = document.activeElement;
      var isFocussedOut = 
       this.$().has(focussedElement).length === 0 && !this.$().is(focussedElement);
      if(isFocussedOut) {
        this.closeOptions({focus:false});
      }
    }, 0);
  }
}.on('focusOut'),

你可以看到它给出错误未捕获TypeError:无法读取未定义的属性has。这是从focusOut方法。当函数执行组件_state是破坏和这个$()返回undefined。

You can see that it gives an error Uncaught TypeError: Cannot read property 'has' of undefined. This is from the focusOut method. By the time the function executes the components _state is 'destroying' and this.$() returns undefined.

我尝试了等待帮手,但仍然无法让测试工作。这通常如何做。
我提取了在bin中运行的测试。 以下是其链接

I tried the wait helper and still I am not able to get the tests to work. How is this normally done. I have extracted the tests to run in a bin. Here is the link to it.

推荐答案

进一步调试后,问题是标签bm-select之一有 focusOut 事件触发的拆卸方法的测试。所以在运行循环代码执行时,组件不在inDOM中。

After further debugging, the problem is one of tags 'bm-select' has it focusOut event triggered in the teardown method of testing. So by the time the run loop code executes the component is not inDOM.

我刚刚在测试应用中添加了一个隐藏的输入字段。一旦我运行所有的测试,我将焦点设置到隐藏的输入字段,并使用等待测试帮助器。现在,所有运行循环代码都是在执行拆卸方法时完成的。

I just added a hidden input field in the test app. Once I've run all the tests, I set focus to the hidden input field and use the wait test helper. Now all the run loop code is done by the time the tear down method is executed.

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

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