我想Ember的集成测试包( http://emberjs.com/guides/testing/integration/ ),但我得到这个错误
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重现此错误: http://jsbin.com/InONiLe/9 ,我们可以通过打开浏览器的控制台中看到。
我认为是什么导致这个错误是线data.set('isLoaded', true);
在load()
的方法App.Posts
。 (链接到代码: http://jsbin.com/InONiLe/9/edit )
现在,如果我包裹data.set('isLoaded', true);
在一个线Ember.run()
那么它会按预期工作和测试将通过。
但是,我使用这个模式很多我的模型,我不想只是包装每.set()
与Ember.run()
过渡也引发了同样的错误)。 我也不想改变应用程序代码为了使测试工作的缘故。
有没有别的东西我可以做些什么来解决这个错误吗?
注:因为否则UI被阻塞,直到承诺解决我故意不返回模型挂钩的承诺。 我想的路线过渡到立即发生,这样我可以显示加载微调。
当您使用一些方法,触发异步代码,如AJAX,setInterval的,则是IndexedDB API等,您将需要委派的论文的方法来解析的回调Ember.run
,所以烬会在你的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
,因为是与Ember超过兼容$.Defered
。 $ .Deferred被返回$.ajax
。
这里是一个更新jsbin http://jsbin.com/InONiLe/10/edit
UPDATE
因为在你的情况,你不想返回一个承诺,所以只是将其删除,只返回数据本身:
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 http://jsbin.com/InONiLe/17/edit
我希望它能帮助
文章来源: ember integration test error. dealing with asynchronous side-effects