试图在Ember中进行验收测试:
test('successful login', (assert) => {
Ember.run(() => {
visit('/signin');
fillIn('#email', 'validemail@server.com');
fillIn('#password', 'password');
click(':submit');
andThen(function() {
assert.equal(currentURL(), '/');
});
});
});
偶尔(并且看似随机)会产生错误:
“全局错误:错误:断言失败:您已打开测试模式,禁用了运行循环的自动运行。您需要在运行中包装任何带有异步副作用的代码......”
我能够获得一个正常工作的版本:
test('successful login', (assert) => {
const done = assert.async();
Ember.run(() => {
visit('/signin').then(() => {
fillIn('#email', 'isaac@silverorange.com').then(() => {
fillIn('#password', 'keen').then(() => {
click(':submit').then(() => {
assert.equal(currentURL(), '/');
done();
});
});
});
});
});
});
但是,如果我包含使用相同路由的第二个测试(对于不成功的登录),其中一个几乎总是以上面列出的错误结束。
我想知道我对运行循环,Ember.run以及如何使用异步行为进行测试并不了解。任何有关良好资源的帮助或指示都将不胜感激!
答案 0 :(得分:0)
根据the guide,您的代码应该是这样的:
test('successful login', (assert) => {
visit('/signin');
fillIn('#email', 'validemail@server.com');
fillIn('#password', 'password');
click(':submit');
andThen(function() {
assert.equal(currentURL(), '/');
});
});
您不需要在案件中添加Ember.run
。
答案 1 :(得分:0)
最常见的情况是,当您在应用程序中执行某些操作(异步)并且没有为Ember正确包装时(包括我的意思是在Ember运行循环中执行),就会出现此问题。
当你导致在runloop(XHR回调或事件处理程序)之外与你的应用程序交互的代码执行时,用Ember.run()包装该代码。
活动:
Ember.$('div').on('mouseover',function() {
Ember.run(function() {
// Interaction with application
});
});
XHR / AJAX:
Ember.$.ajax({
success: function() {
Ember.run(function() {
// Interaction with application
});
}
});