Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 余烬集成测试错误。处理异步副作用_Javascript_Testing_Ember.js_Integration Testing_Ember Testing - Fatal编程技术网

Javascript 余烬集成测试错误。处理异步副作用

Javascript 余烬集成测试错误。处理异步副作用,javascript,testing,ember.js,integration-testing,ember-testing,Javascript,Testing,Ember.js,Integration Testing,Ember Testing,我正在尝试ember的集成测试包(),但是我遇到了这个错误 Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in an Ember.run 我制作了一个JSBin来重现这个错误:,我们可以通过打开浏览器的控制台看到这个错误 我认为导致

我正在尝试ember的集成测试包(),但是我遇到了这个错误

Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun.    
You will need to wrap any code with asynchronous side-effects in an Ember.run
我制作了一个JSBin来重现这个错误:,我们可以通过打开浏览器的控制台看到这个错误

我认为导致此错误的原因是行
data.set('isLoaded',true)App.Posts的
load()
方法中的code>。(链接到代码:)

现在,如果我包装
data.set('isLoaded',true)Ember.run()
中的code>行中,它将按预期工作,测试将通过

但是,我在我的很多模型中都使用了这种模式,我不想用
Ember.run()
来包装每个
.set()
(转换也会触发相同的错误)。我也不想为了使测试正常工作而更改应用程序代码

我还能做些什么来纠正这个错误吗


注意:我故意不在模型钩子中返回承诺,因为否则在承诺得到解决之前,UI将被阻止。我希望立即转换到路由,以便显示加载微调器。

当您使用一些触发异步代码的方法时,如ajax、setInterval、indexeddb api等。您需要将这些方法的已解析回调委托给
Ember.run
,所以,ember将在您的runloop中对这些操作进行排队,并确保应用程序处于同步状态。因此,为此更改代码是正确的处理方法:

App.Posts = Ember.Object.create({
  load: function() {
    return new Ember.RSVP.Promise(function(resolve, reject) {      
      var data = Ember.Object.create();
      $.ajax({
        url: 'https://api.github.com/users/octocat/orgs'
      }).then(function() {
        data.set('isLoaded', true);
        Ember.run(null, resolve, data);        
      }, reject);      
    });    
  }
});
其他建议是始终使用
Ember.RSVP.Promise
,因为它比
$.Defered
$更兼容Ember。延迟由
$返回。ajax

这是一个更新的jsbin

更新

因为在您的情况下,您不想返回承诺,所以请放弃承诺,只需返回数据本身:

App.Posts = Ember.Object.create({
  load: function() {    
    var data = Ember.Object.create();    
    $.ajax({
      url: 'https://api.github.com/users/octocat/orgs'
    }).then(function() {        
      Ember.run(function() {
        data.set('isLoaded', true);
      });                
    }, function(xhr) {        
      Ember.run(function() {
        // if using some ember stuff put here
      });
    });
    return data;
  }
});
下面是jsbin,它显示了这一点


我希望这会有所帮助

我不想在模型挂钩中返回承诺,因为在承诺得到解决之前,转换不会发生。这就是为什么我返回一个Ember对象,这样转换会立即发生,我们会看到加载消息,然后在响应返回时更新dom。