我正在尝试为 webapp 的登录页面创建验收测试。一切几乎都在工作,除了await click(element)
承诺永远不会解决:
import { module, test } from 'qunit';
import { visit, currentURL, fillIn, click, waitFor, getSettledState } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { invalidateSession } from 'ember-simple-auth/test-support';
import { setupMirage } from 'ember-cli-mirage/test-support';
module('Acceptance | login', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
test('login page | login success', async function(assert) {
assert.timeout(5000);
console.log('start', getSettledState());
await visit('/login');
assert.equal(currentURL(), '/login');
await waitFor('.btn-primary:disabled');
await fillIn('input[type="text"]', 'mirage');
await fillIn('input[type="password"]', 'password1234');
await waitFor('.btn-primary:not([disabled])', 2000);
console.log('btn enabled');
let btnSubmit = document.querySelector('.btn-primary');
assert.equal(btnSubmit.disabled, false);
await click(btnSubmit);
console.log('await btn click');
await waitFor('.nav-tabs', 4000);
console.log('nav complete');
assert.equal(currentURL(), '/login-success');
console.log('finished', getSettledState());
});
});
如果我按原样运行此测试,“等待 btn 单击”不会登录控制台,直到超时。我还收到 qunit 错误“Uncaught (in promise) Error: pushFailure() assertion outside test context, in ___ at internalStart”(下划线由我添加)
但是,如果我删除了调用的await
一部分click(btnSubmit)
,则测试成功完成,但是最后一次检查 getSettledState() 将返回:
hasPendingRequests: true
hasPendingTimers: true
hasPendingWaiters: false
hasRunLoop: true
pendingRequestCount: 1
由于有待处理的请求和计时器,即使所有assert()
调用都成功,测试仍然超时。
所以看起来如果我用 正确运行测试,clickawait click(btnSubmit)
() 上的测试超时,但如果我只调用click(btnSubmit)
,测试成功完成,尽管 testem 或 qunit 不知道所有测试已完成。我究竟做错了什么?
海市蜃楼登录端点:
this.post('/login', function(db, request) {
let formData = JSON.parse(request.requestBody);
let auth = this.serialize(db.profiles.all());
if (formData.identification !== auth.data[0].attributes.loginid || formData.password !== auth.data[0].attributes.password) {
return new Response(401, {some: 'header', 'Content-Type': 'application/json'}, {
error_code: "err_loginAuthenticationFail"
});
}
let profile = this.serialize(db.profiles.all());
profile.data[0].attributes.id = profile.data[0].attributes.localid
delete profile.data[0].attributes.localid;
return { ...longAccessTokenObject }
});
Mirage 端点运行正常,它验证我设置的一个用户/密码组合,无论是在测试中还是在 Chrome 中手动使用 /login 页面。