【发布时间】:2016-09-13 11:02:11
【问题描述】:
我目前正在编写一个简单的测试来测试一个 Angular 2 组件,该组件的模板被一个 templateUrl 引用。我目前正在使用 webpack 来执行所需的任务,包括测试。我正在关注webpack guide on the angular site 以及 AngularClass'angular2-webpack-starter repo。
但是,当测试尝试创建组件夹具时,使用
testBed.createComponent(AppComponent);
我收到以下错误:
"'未处理的 Promise 拒绝:', '加载 app.component.html 失败', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', '未能加载 app.component.html',"
因此显然会导致测试失败。
在我的 app.component.ts 中,我通过 templateUrl 引用模板,就像这样
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
title = "Tour of Heroes";
}
我环顾四周,但没有找到解决此错误的方法。请问有人知道这个问题吗?到目前为止我遇到的所有示例(例如这个 repo https://github.com/rangle/angular2-redux-example 中的示例)都包含内联 html。
如果你想查看 webpack / karma 配置文件,这里是我的 repo 的链接。 https://github.com/jeanpaulattard/tourofheroes
编辑:更新了我的仓库主分支的链接。
【问题讨论】:
-
您正在使用模板和样式的相对路径。您可以尝试将
moduleId: module.id属性添加到组件装饰器中吗? Angular 网站上有一篇文章 here -
如果您为 commonjs 设置并使用 Dave V 建议的相对路径,请确保您使用的是
ts转译器,而不是默认的打字稿(忽略 commonjs 打包)。否则,您需要定义绝对路径(从 index.html 的位置开始),即app/app.component.html -
大家好,感谢您的意见!正如 steven 所说, moduleId: module.id 只有在使用 SystemJs 模块加载器时才需要。在我的例子中,因为我使用 webpack 作为打包器和加载器,所以我必须使用上面写的语法。至于问题本身,我将其缩小到这样一个事实,即在我的 webpack.test.ts 文件中,我使用了 awesome-typescript-loader,而不是 ts-loader 链式 angular2-template 加载器。将很快回答问题。谢谢你们的帮助!
标签: angular webpack angular2-components angular2-testing