我正在尝试使用 e2e test angular 7 测试我的服务,我的问题是我不知道该怎么做:
这是我的服务,(方法返回 Observable):
import { Injectable } from '@angular/core';
import { UrlDecoratorService } from "../../common/url-decorator.service";
import { APIFetcherService } from "../common/api-fetcher.service";
import { Observable } from 'rxjs';
import { IALChrono, ALChrono } from '../../common/IALChrono.interface';
@Injectable()
export class AnnonceChronoDetailService {
private months: string[];
constructor(private urlDecoratorService: UrlDecoratorService, private apiFetcher: APIFetcherService) {
}
fetchData(chronoInfo: ALChrono): Observable<any> {
// construct API parameters and URL
var URL: string = this.urlDecoratorService.urlAPIDecorate("AL", "GetAccessChrono");
var params = this.urlDecoratorService.generateParameters({
year: chronoInfo.year,
month: chronoInfo.month,
sortBy: chronoInfo.sortBy,
sortDirection: chronoInfo.sortDirection,
pageNumber: chronoInfo.currentPage,
pageSize: chronoInfo.pageSize
});
return this.apiFetcher.fetchJson(URL, params);
}
}
它在我的服务中还有另外两个服务UrlDecoratorService和APIFetcherService。
这是我的 e2e 测试:
import { AppPage } from './app.po';
import { AnnonceChronoDetailService } from '../../src/app/services/annonce-legale/annonce-chrono-detail.service';
import { ALChrono } from '../../src/app/common/IALChrono.interface';
import { APIResponse } from '../../src/app/common/api-response.interface';
import { Observable } from 'rxjs';
describe('workspace-project App', () => {
let page: AppPage;
let service: AnnonceChronoDetailService;
this.chronoInfo = new ALChrono(); //it's a class
beforeEach(() => {
page = new AppPage();
});
it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
});
it('#getObservableValue should return value from observable', (done: DoneFn) => {
service.fetchData(this.chronoInfo).subscribe((resp: APIResponse) => {
expect(resp.dataCount).toBe(5);
done();
});
});
});
我需要的是如何将两个服务UrlDecoratorService和APIFetcherService注入我的 e2e 测试,或者如何测试注入另一个服务的服务?
如果您需要更多信息,请告诉我。