我有一个名为 logPublisher 的抽象类。下面是代码:
export abstract class LogPublisher {
location: string;
abstract log(record: LogEntry): Observable<boolean>;
abstract clear(): Observable<boolean>;
}
LogWebApi 类扩展了 LogPublisher:
export class LogWebApi extends LogPublisher {
constructor(private http: Http) {
super();
this.location = "http://localhost:59143/api/v1/Log";
}
log(record: LogEntry): Observable<boolean> {
let ret: boolean = false;
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let values: LogEntry[];
return this.http.post(this.location, record, options)
.map(response => response.json)
.catch(this.handleErrors);
}
clear(): Observable<boolean> {
//TODO: call webapi to clear all log entries
return Observable.of(true);
}
private handleErrors(error: any): Observable<any> {
console.log("Error while logging: " + error);
return Observable.throw(error);
}
}
现在在我对 LogWebApi 类的单元测试中,我将如何注入“http”?我正在使用 jasmine 编写单元测试。
任何代码片段都会有所帮助。
提前致谢。