我刚开始为我的角度服务编写单元测试,我正在遇到一个承诺错误。
基本上我有两项服务 1.配置服务 2. CountryDetails服务 国家/地区详细信息服务使用配置服务返回可观察对象。 以下是服务的代码
配置服务
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class ConfigurationService {
private config: any;
constructor(private http: Http) {
}
public loadConfig(): Promise<any> {
return this.http.get('assets/env-config.json')
.map(res => res.json())
.toPromise()
.then(settings => this.config = settings);
}
public get configuration(): any {
return this.config;
}
}
国家详细信息服务
import { Injectable } from '@angular/core';
import { Country } from '../models/country';
import {ConfigurationService } from '../../services/configuration.service';
import { Observable } from 'rxjs/Observable';
import { HttpClient} from '@angular/common/http';
@Injectable()
export class CountryDetailsService {
public countryData: Country;
constructor(
private http: HttpClient,
private configSvc: ConfigurationService
) { }
getData(isoCode: string): Observable<Country> {
const endpointUrl = `${this.configSvc.configuration.CountryAPIUrl}Details/${isoCode}`;
const country$ = this.http.get<Country>(endpointUrl);
country$.subscribe(rslt => this.countryData = rslt);
return country$;
}
}
测试CountryDetails
import { TestBed, getTestBed, inject, async } from '@angular/core/testing';
import { CountryDetailsService } from './countrydetails.service';
import {ConfigurationService } from '../../services/configuration.service';
import { Country } from '../models/country';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Http } from '@angular/http';
fdescribe('CountryDetailsService', () => {
let httpClient: HttpClient;
let httpTestingController: HttpTestingController;
let countrySvc: CountryDetailsService;
let configSvc: ConfigurationService;
let http = TestBed.get(Http);
let expectedcountryData: Country ;
const inputIsoCode = 'IN';
beforeEach( async (() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [CountryDetailsService, ConfigurationService ]
});
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
countrySvc = TestBed.get(CountryDetailsService);
configSvc = new ConfigurationService(http);
configSvc.loadConfig();
countrySvc = TestBed.get(CountryDetailsService);
expectedcountryData = {
CountryId: 13415,
IsoCode2: 'IN',
IsoCode3: 'IND',
LongName: 'India, Republic of',
Name: 'India',
Capital: 'New Delhi',
NationalDay: '26 January',
NumberOfAccession: 18,
Ambassador: null,
CurrentBoardMember: 'Y',
MembershipStartDate: '1957-07-16T00:00:00'
};
}));
afterEach(async(() => {
httpTestingController.verify();
}));
it('should return expected country (called once)', async( () => {
countrySvc.getData(inputIsoCode).subscribe(
countryData => {
expect(countryData).toEqual(expectedcountryData, 'should return expected country i.e IN');
}
);
const req = httpTestingController.expectOne(`${this.configSvc.configuration.CountryAPIUrl}Details/${inputIsoCode}`);
expect(req.request.method).toEqual('GET');
req.flush(expectedcountryData);
}));
});
错误
Error: Cannot call Promise.then from within a sync test.
Error: Cannot call Promise.then from within a sync test.
at SyncTestZoneSpec.webpackJsonp.../../../../zone.js/dist/sync-test.js.SyncTestZoneSpec.onScheduleTask ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/zone.js/dist/sync-test.js:30:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/zone.js/dist/zone.js:405:1)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/zone.js/dist/zone.js:236:1)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleMicroTask ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/zone.js/dist/zone.js:256:1)
at scheduleResolveOrReject ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/zone.js/dist/zone.js:871:1)
at ZoneAwarePromise.then ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/zone.js/dist/zone.js:981:1)
at ApplicationInitStatus.webpackJsonp.../../../core/esm5/core.js.ApplicationInitStatus.runInitializers ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/@angular/core/esm5/core.js:3457:1)
at TestBed.webpackJsonp.../../../core/esm5/testing.js.TestBed._initIfNeeded ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/@angular/core/esm5/testing.js:964:1)
at TestBed.webpackJsonp.../../../core/esm5/testing.js.TestBed.get ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/@angular/core/esm5/testing.js:1000:1)
at Function.webpackJsonp.../../../core/esm5/testing.js.TestBed.get ( ://localhost:9876/_karma_webpack_/webpack:/C:/VSO/CountryProfiles AngularApp/Iaea.CountryProfiles.Web/node_modules/@angular/core/esm5/testing.js:805:1)
I would be grateful if someone can point out the exact problem in this