我最近将 Ember 应用程序从 2.18 升级到了 3.13,运行顺利。今天我第一次尝试添加验收测试(在此之前只有集成/单元测试),但测试在第一行失败:
import { module, test } from "qunit";
import { visit, currentURL } from "@ember/test-helpers";
import { setupApplicationTest } from "ember-qunit";
module("Acceptance | some route", function(hooks) {
setupApplicationTest(hooks);
test("visiting /some-route", async function(assert) {
await visit("/some-route"); // <----- error thrown here
assert.equal(currentURL(), "/some-route");
});
});
我看到了几个错误(按此顺序):
Source:
TypeError: Cannot read property 'lookup' of undefined
at Object.initialize (http://localhost:4200/assets/my-app.js:10312:28)
at http://localhost:4200/assets/vendor.js:61627:21
at Vertices.each (http://localhost:4200/assets/vendor.js:80243:9)
at Vertices.walk (http://localhost:4200/assets/vendor.js:80157:12)
at DAG.each (http://localhost:4200/assets/vendor.js:80087:22)
at DAG.topsort (http://localhost:4200/assets/vendor.js:80095:12)
at Class._runInitializer (http://localhost:4200/assets/vendor.js:61653:13)
at Class.runInitializers (http://localhost:4200/assets/vendor.js:61625:12)
at Class._bootSync (http://localhost:4200/assets/vendor.js:59923:14)
at Class.boot (http://localhost:4200/assets/vendor.js:59890:14)
Source:
Error: Cannot call `visit` without having first called `setupApplicationContext`.
at visit (http://localhost:4200/assets/test-support.js:44177:13)
at Object._callee$ (http://localhost:4200/assets/tests.js:23:47)
at tryCatch (http://localhost:4200/assets/vendor.js:12365:40)
at Generator.invoke [as _invoke] (http://localhost:4200/assets/vendor.js:12591:22)
at Generator.prototype.<computed> [as next] (http://localhost:4200/assets/vendor.js:12417:21)
at asyncGeneratorStep (http://localhost:4200/assets/tests.js:6:105)
at _next (http://localhost:4200/assets/tests.js:8:196)
at http://localhost:4200/assets/tests.js:8:366
at new Promise (<anonymous>)
at Object.<anonymous> (http://localhost:4200/assets/tests.js:8:99)
经过一番挖掘,看起来有些东西正在错误地设置测试上下文。它只是一个空对象:
因此,isApplicationTestContext(context)
返回 false 并引发第二个错误。我猜第一个错误被抛出是因为应用程序有一些执行查找的初始化程序。
为了添加此验收测试,我还将test-helper.js
文件更新为以下内容:
import Application from "../app";
import config from "../config/environment";
import { setApplication } from "@ember/test-helpers";
import { start } from "ember-qunit";
setApplication(Application.create(config.APP));
start();
使用上面的文件,所有测试都失败了,所以似乎setApplication
导致测试上下文设置不正确?旧test-helper.js
文件是这样的:
import resolver from "./helpers/resolver";
import { setResolver } from "@ember/test-helpers";
import { start } from "ember-cli-qunit";
setResolver(resolver);
start();
我尝试重新添加setResolver
呼叫,但没有任何区别。有没有其他人使用新的 ember-qunit 语法遇到这些问题,或者可能会看到我做错了什么?另外,我autoboot = false;
在environment.js
文件中设置了没有区别的文件。测试套件还有一两个测试仍然用旧的 ember-qunit 语法编写。任何帮助,将不胜感激!