我有这个测试。在那里我设置了我的组件的一些值。这些值确定按钮的状态。在 fixture.detectChanges() 之前,这些值仍然设置。但是在(就在 const createButton ....)之后,这些值消失了并再次设置为 null。问题是为什么以及如何设置这些值,以便我能够检测到按钮状态的变化。
it('should activate the create button with a customer and service set', () => {
fixture = TestBed.createComponent(MyComponent);
fixture.componentInstance.entry.service = new Service('1', 'Name', null);
fixture.componentInstance.entry.customer = customerMock;
fixture.detectChanges();
const createButton = fixture.debugElement.query(By.css('p-button[name="createButton"]'));
const actual = createButton.attributes['ng-reflect-disabled']; // TODO try find better way
expect(actual).toBe('false');
});
Run Code Online (Sandbox Code Playgroud)
编辑:根据要求。我不能发布整个组件,但我认为该组件的相关部分是 onInit。
ngOnInit() {
this.servicePoolSubscription = this.stateService.getServiceSubject()
.subscribe(serviceList => this.services = serviceList);
this.Entry = EntryService.prepare();
}
ngOnDestroy(): void {
if (this.serviceSubscription) {
this.serviceSubscription.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所想。EntryService.prepare() 准备一个新对象。fixture.detectChanges 会再次触发 onInit 吗?