это мой первый модульный тест в угловых 7, я хочу проверить мой метод в моей службе, этот метод возврата Наблюдаемый :
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,//Année
month: chronoInfo.month,//Mois (peut être null)
sortBy: chronoInfo.sortBy,// Champ de tri
sortDirection: chronoInfo.sortDirection,//True pour inverser l'ordre de tri
pageNumber: chronoInfo.currentPage,//Page de résultats
pageSize: chronoInfo.pageSize//Nb enregistrements par page
});
return this.apiFetcher.fetchJson(URL, params);
//retourne les AL par année & mois
}
и тест этого метода:
import { TestBed, async, inject } from '@angular/core/testing';
import { AnnonceChronoDetailService } from './annonce-chrono-detail.service';
import { HttpClientModule } from '@angular/common/http';
import { UrlDecoratorService } from "src/app/common/url-decorator.service";
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { APIFetcherService } from "src/app/services/common/api-fetcher.service";
import { Http, ConnectionBackend, RequestOptions, HttpModule } from "@angular/http";
import { ALChrono } from '../../common/IALChrono.interface';
import { Observable } from 'rxjs';
describe('AnnonceChronoDetailService', () => {
let service: AnnonceChronoDetailService;
let alChrono: ALChrono = { //it's an interface'
year: 2012,
month: -1,
sortBy: 'asc',
sortDirection: true,
currentPage: 1,
pageSize: 15
}
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
AnnonceChronoDetailService,
UrlDecoratorService,
APIFetcherService,
Http,
ConnectionBackend]
});
});
it('should be created', inject([AnnonceChronoDetailService], (service: AnnonceChronoDetailService) => {
expect(service).toBeTruthy();
}
));
it('#fetchData should return value from observable', (done: DoneFn) => {
service.fetchData(alChrono).subscribe(value => {
expect(value).toBe('observable value');
done();
});
});
});
и когда я выполняю ng test
, у меня проблема во втором тесте #fetchData should return value from observable
, ошибка:
AnnonceChronoDetailService > #fetchData should return value from observable
TypeError: Cannot read property 'fetchData' of undefined
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/src/app/services/annonce-legale/annonce-chrono-detail.service.spec.ts?:41:17)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/node_modules/zone.js/dist/zone.js?:388:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/node_modules/zone.js/dist/zone-testing.js?:288:1)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/node_modules/zone.js/dist/zone.js?:387:1)
at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/node_modules/zone.js/dist/zone.js?:138:1)
at runInTestZone (http://localhost:9876/node_modules/zone.js/dist/zone-testing.js?:506:1)
at UserContext.<anonymous> (http://localhost:9876/node_modules/zone.js/dist/zone-testing.js?:522:1)
at <Jasmine>
Я пробую некоторые решения, такие как добавление asyn
, но проблема была та же.