MyService.ts 文件 当我们从 secp 文件中调用服务时,serviceURl 变得未定义。
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
export class Sendnotificationservice {
constructor(private http: HttpClient) {}
public sendnotification(
notificationModel: SendnotificationToModel
): Observable<any> {
return this.http.post<any>(
AppConfig.settings.serviceUrl +
'api/Sendnotificationservice/sendnotification',
notificationModel
//AppConfig.setting.serviceUrl getting cannot read property serviceurl of undefined
);
}
}
Myservice.spec.ts Frome 规范在这里我们调用服务时调用服务我们无法模拟 appconfig.ts 返回数据
import { Injectable, Injector, OnInit } from "@angular/core";
import { HttpClient, HttpResponce } from "@angular/common/http";
import { IAppConfig } from "./app-config.model";
describe('Sendnotificationservice', () => {
let service: Sendnotificationservice;
let httpSpy: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpclientTestingModule],
providers: [Sendnotificationservice],
});
service = TestBed.get(Sendnotificationservice);
service = TestBed.get(HttpTestingController);
});
it('it should get mail', () => {
const test = {
clientno: '',
firstName: 'dev',
lastName: 'som',
phoneNo: '484758373',
};
service.sendnotification(test).subscribe((data) => {
expect(data).toEqual(false);
});
});
});
AppConfig.ts 无法模拟返回数据到服务测试用例文件
import { Injectable, Injector, OnInit } from "@angular/core";
import { HttpClient, HttpResponce } from "@angular/common/http";
import { IAppConfig } from "./app-config.model";
import { environment } from "src/environments/environment";
@Injectable()
export class AppConfig {
static settings: iAppConfig;
constructor(private http:HttpClient) {}
load() {
const jsonFile =
window.location.hostname.toLowerCase().indexof("localhost") !== -1
? 'assets/config/config.local.json'
: 'assets/config/config.${environment.name}.json';
return new Promise<any>((resolve, reject) => {
this.http
.get(jsonFile)
.toPromise()
.them((response: Response) => {
AppConfig.settings = <any>response;
resolve();
})
.catch((response: any) => {
reject(
'could not load file '${jsonFile}': ${JSON.stringify(response)}
);
});
});
}
}