【发布时间】:2019-01-31 12:57:39
【问题描述】:
我有这个代码:
export class ProgramComponent implements OnInit {
@Input() events: Observable<any>;
eventsSubscription: any;
...
ngOnInit() {
this.eventsSubscription = this.events.subscribe((event) => {
... <- Some code that I want to test!!!!
console.log("The test doesn't get past here!!!!");
});
}
}
describe('BLA BLA BLA', () => {
let component: ProgramComponent;
let fixture: ComponentFixture<ProgramComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
...
],
declarations: [ProgramComponent],
providers: [
...
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProgramComponent);
component = fixture.componentInstance;
// HERE I WANT TO SPY ON THAT EVENTS OBSERVABLE AND RETURN SOME VALUE
// I tried this without success
spyOn(component, 'events').and.returnValue({type: 'somevalue'}))
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
问题是fixture.detectChanges();不会触发可观察事件的订阅。我必须使用 spyOnProperty 吗?但它是组件的输入...
谢谢!
【问题讨论】:
标签: angular testing jasmine observable subscribe