Angular 2 次测试
Angular 2 testing
我有一个组件。他的主要角色是包装。
有什么麻烦?
当方法 compileComponents 执行组件时没有 属性 title。这就是为什么,正如我所想,在控制台中我看到错误
问题是:我如何才能先绑定 属性 然后 运行 compileComponents 方法?
组件模板:
<div class="card">
<div *ngIf="title" class="card-header clearfix">
<h3 class="card-title">{{ title }}</h3>
</div>
<div class="card-body">
<ng-content></ng-content>
</div>
</div>
描述部分:
describe('CardComponent', () => {
let comp: CardComponent;
let fixture: ComponentFixture<CardComponent>;
let titleEl: DebugElement; // the DebugElement with the welcome message
// async beforeEach
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [ CardComponent ],
}).compileComponents(); // compile template and css
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(CardComponent);
comp = fixture.componentInstance;
titleEl = fixture.debugElement.query(By.css('.card-title'));
comp.title = "Greatest title";
fixture.detectChanges(); // trigger initial data binding
});
it('Card check title', () => {
expect(titleEl.nativeElement.textContent).toContain("Greatest title");
});
});
这是因为您试图在元素存在之前就获取该元素。 title
确定 *ngIf
元素可见。在检测到更改后尝试将元素移动到
beforeEach(() => {
fixture = TestBed.createComponent(CardComponent);
comp = fixture.componentInstance;
// titleEl = fixture.debugElement.query(By.css('.card-title'));
comp.title = 'Greatest title';
fixture.detectChanges(); // trigger initial data binding
titleEl = fixture.debugElement.query(By.css('.card-title'));
});
我有一个组件。他的主要角色是包装。
有什么麻烦?
当方法 compileComponents 执行组件时没有 属性 title。这就是为什么,正如我所想,在控制台中我看到错误
问题是:我如何才能先绑定 属性 然后 运行 compileComponents 方法?
组件模板:
<div class="card">
<div *ngIf="title" class="card-header clearfix">
<h3 class="card-title">{{ title }}</h3>
</div>
<div class="card-body">
<ng-content></ng-content>
</div>
</div>
描述部分:
describe('CardComponent', () => {
let comp: CardComponent;
let fixture: ComponentFixture<CardComponent>;
let titleEl: DebugElement; // the DebugElement with the welcome message
// async beforeEach
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [ CardComponent ],
}).compileComponents(); // compile template and css
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(CardComponent);
comp = fixture.componentInstance;
titleEl = fixture.debugElement.query(By.css('.card-title'));
comp.title = "Greatest title";
fixture.detectChanges(); // trigger initial data binding
});
it('Card check title', () => {
expect(titleEl.nativeElement.textContent).toContain("Greatest title");
});
});
这是因为您试图在元素存在之前就获取该元素。 title
确定 *ngIf
元素可见。在检测到更改后尝试将元素移动到
beforeEach(() => {
fixture = TestBed.createComponent(CardComponent);
comp = fixture.componentInstance;
// titleEl = fixture.debugElement.query(By.css('.card-title'));
comp.title = 'Greatest title';
fixture.detectChanges(); // trigger initial data binding
titleEl = fixture.debugElement.query(By.css('.card-title'));
});