所以我正在测试我的 angular2 组件和服务。
到目前为止,我已经使用 mockBackend 来模拟服务中的 get 请求,如下所示:
/* tslint:disable:no-unused-variable */
import { MockBackend } from '@angular/http/testing';
import { Http, ConnectionBackend, BaseRequestOptions, Response, ResponseOptions } from '@angular/http';
import { PagesService } from './pages.service';
import { tick, fakeAsync } from '@angular/core/testing/fake_async';
import { inject, TestBed } from '@angular/core/testing/test_bed';
import {GlobalService} from './../../shared/global.service';
describe('PagesService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: Http, useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
{ provide: PagesService, useClass: PagesService },
{ provide: GlobalService, useClass: GlobalService },
{ provide: MockBackend, useClass: MockBackend },
{ provide: BaseRequestOptions, useClass: BaseRequestOptions }
]
});
});
//should retrive all search results
it('should retrieve all search results',
inject([PagesService, MockBackend], fakeAsync((pagesService: PagesService, mockBackend: MockBackend) => {
let res: Response;
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe('http://localhost:3200/pm/pages/');
let response = new ResponseOptions({
body: '[{"name": "Rapid"}, {"name": "NGBC"}]'});
c.mockRespond(new Response(response));
});
pagesService.getAllpages().subscribe((response) => {
res = response;
});
tick();
expect(res[0].name).toBe('Rapid');
}))
);
it('should fetch by Page id',
inject([PagesService, MockBackend], fakeAsync((pagesService: PagesService, mockBackend: MockBackend) => {
let res;
mockBackend.connections.subscribe(c => {
let page_id:string='2';
expect(c.request.url).toBe('http://localhost:3200/pm/pages/'+page_id);
let response = new ResponseOptions({body: '[{"id": 1, "name": "Rapid"}, {"id": 2, "name": "NGBC"}]'});
c.mockRespond(new Response(response));
});
pagesService.getPageById('2').subscribe((response) => {
res = response;
});
tick();
expect(res[1].name).toBe('NGBC');
}))
);
});
如果你想看到我的 service.ts 如下:
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 + '/pm/pages/')
.map((res: Response) => res.json()).catch(this.handleError);
}
getPageById(page_id: string) {
return this.http.get(this.baseUrl + '/pm/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+'/pm/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+'/pm/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+'/pm/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 + '/pm/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');
}
}
从上述服务中,我已经在我的 service.spec.ts 文件中正确实现了 getAllPages() 和 getPageById() 方法,因为它们工作正常。
我想知道如何为方法实现测试用例savePage() , updatePage() , deletePage()
。
有什么输入吗?
提前致谢。