属性'then'在void类型上不存在,Angular 6单元测试错误

时间:2019-12-29 01:46:04

标签: unit-testing angular6 karma-runner

(angular 6应用程序,使用业力和茉莉花进行单元测试)
你好,
作为单元测试的新手,我通过复制并粘贴在教程中找到的代码,尝试对Service的功能之一进行http测试:

let httpClientSpy: { get: jasmine.Spy };
import { TestBed, async, inject } from '@angular/core/testing';
import { TrainerCalendarService } from './trainer-calendar.service';
import { asyncData } from '../helpers/async-observable-helpers';
import { Appointment } from '../models/appointment';

let service: TrainerCalendarService;

describe('Service: TrainerCalendar', () => {
beforeEach(() => {
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get', 'post']);
service = new TrainerCalendarService(<any> httpClientSpy);
 });

it('should returns list of appointment', () => {
const list: Appointment[] = [
  new Appointment(),
  new Appointment()
];

httpClientSpy.get.and.returnValue(asyncData<Appointment[]>(list));

service.getAll().then(items => expect(items).toEqual(list, 'same items'));
});
});

根据我的应用更改它后,我得到了这个信息:

import { grpService } from "./app/Services/grp.service"; 
import { of } from 'rxjs'
import { EtudiantComponent } from './app/etudiant/etudiant.component';

let httpClientSpy: { get: jasmine.Spy };

let service: grpService;

describe('Service: Test attendency ', () => {
beforeEach(() => {
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get', 'post']);
service = new grpService(<any> httpClientSpy);
 });

 it('should returns list of students', () => {
 const list: EtudiantComponent[] = [
  new EtudiantComponent(service),
  new EtudiantComponent(service)
];

httpClientSpy.get.and.returnValue(of<EtudiantComponent[]>(list));

service.getGroupesFromServer.then(items => expect(items).toEqual(list, 'same items'));
 });
});

我在最后一行代码中遇到错误:类型'()=> void'上不存在属性'then'。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

试图通过我的方式,它显然是通过将该行(出现错误的地方)分解为多个不太复杂的代码行来工作的:

httpClientSpy.get.and.returnValue(of<EtudiantComponent[]>(list));

let listres : EtudiantComponent[]; 
service.getGroupesFromServer(); 
let items = service.tabres ; 
expect(items).toEqual(list, 'same items');