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

问题描述 投票:0回答:1

(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'。

我该如何解决?

unit-testing angular6 karma-runner
1个回答
0
投票

[试图通过我的方式,并且显然通过将那行(我得到错误的地方)分解为多行不太复杂的代码而起作用:

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

let listres : EtudiantComponent[]; 
service.getGroupesFromServer(); 
let items = service.tabres ; 
expect(items).toEqual(list, 'same items');
© www.soinside.com 2019 - 2024. All rights reserved.