This is the code where it is breaking:
**ngOnInit() {
this.service.getTodosPromise().then(t => {
this.todos = t; });
}**
and this is the getTodosPromise() method in the Service:
**getTodosPromise() {
return this.http.get('...').pipe(map(r => r.json())).toPromise();
}**
And in the *.spect.ts file i have one test for which it is breaking:
**it('should load todos from the server', async(() => {
const service = TestBed.get(TodoService);
// This is used if provider is given at component level.
fixture.debugElement.injector.get(TodoService);
spyOn(service, 'getTodosPromise').and.returnValue(from(Promise.resolve([1, 2, 3])));
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.todos.length).toBe(3);
console.log('EXPECT WAS CALLED1');
});
}));**
我正在使用angular 6,没有看到任何编译错误,但是由于上述错误,我的测试用例失败了。
答案 0 :(得分:1)
只需删除from
。它返回Observable而不是Promise
spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve([1, 2, 3]));
答案 1 :(得分:1)
您不应使用from
。它返回observable
:
spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve([1, 2, 3]));