Unit testing Angular 2单元测试:当子组件绑定到带有“quot;的css类时,子组件模板不更新&引用;选择器?
请让我的PRUNKR测试通过-。子组件必须绑定到tr的class属性,并且这种类型的*ngFor也是必需的 这是我的父组件:Unit testing Angular 2单元测试:当子组件绑定到带有“quot;的css类时,子组件模板不更新&引用;选择器?,unit-testing,angular,Unit Testing,Angular,请让我的PRUNKR测试通过-。子组件必须绑定到tr的class属性,并且这种类型的*ngFor也是必需的 这是我的父组件: @Component({ selector: 'my-cmp', template: `<div (click)="updateMe()"></div><tr class="child-cmp" *ngFor="let _testVar of _testVars" [_testVar]="_testVar">` }) export
@Component({
selector: 'my-cmp',
template: `<div (click)="updateMe()"></div><tr class="child-cmp" *ngFor="let _testVar of _testVars" [_testVar]="_testVar">`
})
export class myCmp {
_testVars = ["initial value"];
private updateMe(): void {
this._testVars = ["updated value"];
}
}
@组件({
选择器:“我的cmp”,
模板:``
})
导出类myCmp{
_testVars=[“初始值”];
私有updateMe():void{
这个._testVars=[“更新值”];
}
}
我只想让这个测试起作用:
describe("trying a test", () => {
beforeEach(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
TestBed.configureTestingModule({
declarations: [myCmp, ChildCmp]
});
});
it("test should work", () => {
const fixture = TestBed.createComponent(myCmp);
fixture.detectChanges();
const div = fixture.debugElement.children[0];
const childCmp = div.queryAll(By.directive(ChildCmp));
const divEl = div.queryAll(By.css('div'));
divEl[0].triggerEventHandler('click', <Event>{});
fixture.detectChanges();
expect(childCmp[0].nativeElement.textContent).toBe("updated value");
expect(true).toBe(true);
});
});
description(“尝试测试”,()=>{
在每个之前(()=>{
initTestEnvironment(BrowserDynamicTestingModule,platformBrowserDynamicTesting());
TestBed.configureTestingModule({
声明:[myCmp,ChildCmp]
});
});
它(“测试应该有效”,()=>{
const fixture=TestBed.createComponent(myCmp);
fixture.detectChanges();
const div=fixture.debugElement.children[0];
const childCmp=div.queryAll(按指令(childCmp));
const divEl=div.queryAll(By.css('div'));
divEl[0].triggerEventHandler('click',{});
fixture.detectChanges();
期望(childCmp[0].nativeElement.textContent).toBe(“更新值”);
期望(真),期望(真);
});
});