[英]Angular2 testing error when using templateUrl
I am writing some tests for my Angular2 application according to the documentation but I have run into a problem which I can't seem to fix. 我正在根据文档为Angular2应用程序编写一些测试,但遇到了一个似乎无法解决的问题。 I get the following error when trying to launch the spec runner:
尝试启动规范运行器时出现以下错误:
Failed: This test module uses the component CategoriesComponent which is using a "templateUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test.
I understand this is happening as I am using a seperate template file for the template within the component but I jhave tried multilpe solutions which don't seem to work. 我知道这种情况正在发生,因为我在组件中使用单独的模板文件作为模板,但是我尝试过似乎不起作用的多解决方案。
Here is my component under test: 这是我正在测试的组件:
import { Component } from "@angular/core";
@Component({
selector: 'categories-component',
templateUrl: '/app/views/catalog/categories/categories-dashboard.html',
moduleId: module.id
})
export class CategoriesComponent {
title: 'Categories;
}
The categories-dashboard.html file: Categories-dashboard.html文件:
<h1>{{ title }}</h1>
and my testing module for the component: 和我对该组件的测试模块:
import {TestBed, ComponentFixture, ComponentFixtureAutoDetect, async} from "@angular/core/testing";
import { By} from "@angular/platform-browser";
import { CategoriesComponent } from "../../../../components/catalog/categories/CategoriesComponent";
import { DebugElement } from "@angular/core";
let comp: CategoriesComponent;
let fixture: ComponentFixture<CategoriesComponent>;
let de: DebugElement;
let el: HTMLElement;
describe('BannerComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CategoriesComponent ],
providers: [
{ provide: ComponentFixtureAutoDetect, useValue: true }
]
});
TestBed.compileComponents();
fixture = TestBed.createComponent(CategoriesComponent);
comp = fixture.componentInstance; // BannerComponent test instance
// query for the title <h1> by CSS element selector
de = fixture.debugElement.query(By.css('h1'));
el = de.nativeElement;
}));
it('should display original title', () => {
expect(el.textContent).toContain(comp.title);
});
});
I have tried to implement TestBed.compileComponents() into into the component but wherever I put it it doesn't seem to work. 我试图将TestBed.compileComponents()实现到组件中,但是无论我放在哪里,它似乎都不起作用。
Can anyone see why this error is occurring or point me in the directoin of a solution? 谁能看到为什么会发生此错误,或者让我指出解决方案的方向?
Thanks! 谢谢!
compileComponents
resolves asynchronously (as it makes an XHR for the template), so it returns a promise. compileComponents
异步解析(因为它为模板生成XHR),因此它返回一个compileComponents
。 You should handle anything requiring the resolution of the promise, inside of the then
callback of the promise 您应该在promise的
then
回调内部处理所有需要解决promise的事情
TestBed.compileComponents().then(() =>{
fixture = TestBed.createComponent(CategoriesComponent);
comp = fixture.componentInstance; // BannerComponent test instance
// query for the title <h1> by CSS element selector
de = fixture.debugElement.query(By.css('h1'));
el = de.nativeElement;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.