【发布时间】:2019-01-22 22:55:25
【问题描述】:
我有这个代码。我将myData 保存在ngOnInit 中。当我通过单击按钮测试 myMethod 时,this.myData 在 myMethod 中为 undefined。
@Component({
templateUrl: ''
})
export class MyComponent implements OnInit {
myData: MyClass;
constructor() {}
ngOnInit() {
this.myData = // some code
}
myMethod() {
this.myData is undefined here
}
}
我的规范文件是:
describe('MyComponent', () => {
// set up stubs
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
],
imports: [
FormContainer
],
schemas: [NO_ERRORS_SCHEMA],
})
.compileComponents();
fixture = TestBed.createComponent(MyComponent);
comp = fixture.debugElement.componentInstance;
}));
it('should call myMethod', fakeAsync(() => {
element.querySelectorAll('button')[0].click();
fixture.detectChanges();
fixture.whenStable().then(async() => {
fixture.detectChanges();
});
}));
});
【问题讨论】:
-
您是否将
myData设置为 MyClass 属性? -
@codeepic 是的,我已经更新了问题,程序编译正常。
标签: angular jasmine components