Я новичок в тесте карма-жасмин, и я борюсь с тестом, который всегда выдает эту ошибку при запуске теста:
Ошибка: не удается вызвать Promise.then из теста синхронизации.
Я использую угловой 5. Вот мой тестовый код:
fdescribe('CommentComponent', () => {
let component: CommentComponent;
let fixture: ComponentFixture<CommentComponent>;
let commentService: CommentService;
const stationId = 900;
let station: Station;
let comment: Comment;
beforeEach(fakeAsync(() => {
station = new Station();
station.id = stationId;
comment = new Comment('', null, null, '');
TestBed.configureTestingModule({
declarations: [
CommentComponent,
],
providers: [
CommentService,
StationService,
],
imports: [
HttpClientTestingModule,
FormsModule,
RouterTestingModule,
MatSnackBarModule,
NgxPaginationModule,
BrowserAnimationsModule
],
schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents().then(() => {
fixture = TestBed.createComponent(CommentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
}));
afterEach(fakeAsync(() => {
component = null;
fixture.destroy();
commentService = null;
comment = null;
}));
commentService = TestBed.get(CommentService);
it('should create', async() => {
tick(1000);
expect(component).toBeTruthy();
});
});
Вот мой код компонента:
@Component({
selector: 'app-comment',
templateUrl: './comment.component.html',
styleUrls: ['./comment.component.css']
})
export class CommentComponent implements OnInit {
@ViewChild('newCommentForm') form;
params: Params;
comment: Comment = new Comment('', null, null, '');
comments = [];
constructor(private commentService: CommentService,
private route: ActivatedRoute,
public snackBar: MatSnackBar) {this.route.params.subscribe( params => this.params = params);
}
ngOnInit() {
this.getComment();
}
onSubmit() {
this.commentService.saveComment(this.comment.text, this.params['id']).subscribe(
comment => this.comment = comment,
error => this.onErrorSave(error) ,
() => this.onCompleteSave()
);
}
getComment(): void {
this.commentService.getComments(this.params['id']).subscribe(
comments => this.comments = comments,
error => console.error(error),
() => console.log('Commentaires chargés')
);
}
onErrorSave(error) {
this.snackBar.open(errorMessageConnection, 'Close', {
duration: 3000
});
console.error(error);
}
onCompleteSave() {
this.snackBar.open(sendingMsgCommentSucces, 'Close', {
duration: 3000
});
console.log('Commentaire sauvegardé');
this.form.reset();
this.getComment();
}
}
Если кто-нибудь сможет мне помочь, это будет оценено.
Заранее спасибо.