【发布时间】:2020-02-01 09:07:31
【问题描述】:
我最近将 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 语法遇到这些问题,或者可能会看到我做错了什么?另外,我在environment.js 文件中设置了autoboot = false;,这并没有什么不同。测试套件还有一两个测试仍然用旧的 ember-qunit 语法编写。任何帮助将不胜感激!
【问题讨论】:
-
我的猜测是你在从 2.18 升级到 3.13 的过程中忘记了一些东西。测试上下文应在您的验收测试中由
setupApplicationTest(hooks);设置。见github.com/emberjs/ember-qunit/blob/v4.5.1/addon-test-support/…。你用的是什么版本的ember-qunit? -
我会根据默认蓝图仔细检查您的应用程序。你可以在ember-cli/ember-new-output repo 上找到它。我会特别仔细检查您的
config/environment.js、依赖版本和tests/文件夹中的所有文件。我将首先验证APP.autoboot在测试环境中是false:github.com/ember-cli/ember-new-output/blob/… -
@JulienPalmas 我正在使用
ember-qunit版本4.5.1。在您链接的行处暂停执行时this只是一个 POJO,所以我猜有些东西事先设置不正确? -
@jelhan 我仔细检查了默认蓝图,它看起来确实一样。
environment.js文件似乎没有什么特别之处,我确实将APP.autoboot设置为false用于测试环境。
标签: ember.js qunit ember-qunit ember-octane