我现在正在学习 angular2 RC5。我想在 angular2 RC5 中测试以下服务,但我无法以某种方式对其进行配置。
import { Injectable } from '@angular/core';
import { Http, Response,Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {Observable} from 'rxjs/Observable';
import {GlobalService} from './../../shared/global.service';
@Injectable()
export class PagesService {
private headers = new Headers();
public baseUrl:string;
constructor(private http: Http,private _globalService:GlobalService) {
this.baseUrl=this._globalService.baseUrl;
}
getAllpages() {
return this.http.get(this.baseUrl + '/pages/')
.map((res: Response) => res.json()).catch(this.handleError);
}
getPageById(page_id: string) {
return this.http.get(this.baseUrl + '/pages/' + page_id)
.map((res: Response) => res.json()).catch(this.handleError);
}
savePage(page: Object) {
this.headers=new Headers();
this.headers.append('Content-Type', 'application/json');
let url = this.baseUrl+'/pages/';
let data={};
data["data"]=page;
return this.http.post(url, JSON.stringify(data),{headers: this.headers})
.map((res: Response) => res.json()).catch(this.handleError);
}
updatePage(page: any) {
this.headers=new Headers();
this.headers.append('Content-Type', 'application/json');
let url = this.baseUrl+'/pages/'+page._id;
let data={};
data["data"]=page;
return this.http.put(url, JSON.stringify(data),{headers: this.headers})
.map((res: Response) => res).catch(this.handleError);
}
deletePage(page_id: string) {
this.headers=new Headers();
this.headers.append('Content-Type', 'application/json');
let url = this.baseUrl+'/pages/';
return this.http.delete(url + page_id,{headers: this.headers,body: ''})
.map((res: Response) => res).catch(this.handleError);
}
mergePage(page: Object) {
return this.http.post(this.baseUrl + '/pages/',JSON.stringify(page))
.map((res: Response) => res.json()).catch(this.handleError);
}
handleError(error: any) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
我想测试这个服务中的每个方法,有什么输入吗?