2

我现在正在学习 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');
   }
}

我想测试这个服务中的每个方法,有什么输入吗?

4

1 回答 1

1

要测试 Angular2 服务,您需要在单元测试中导入它,然后一个接一个地测试方法:

describe('Service: LanguagesService', () => {   let service;
     //setup   beforeEachProviders(() => [
    LanguagesService   ]);
     beforeEach(inject([LanguagesService], s => {
    service = s;   }));
     //specs   it('should return available languages', () => {
    let languages = service.get();
    expect(languages).toContain('en');
    expect(languages).toContain('es');
    expect(languages).toContain('fr');
    expect(languages.length).toEqual(3);   }); })

真正有用的链接:

https://angular.io/docs/ts/latest/testing/ http://twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/

https://developers.livechatinc.com/blog/testing-angular-2-apps-dependency-injection-and-components/

https://medium.com/google-developer-experts/angular-2-unit-testing-with-jasmine-defe20421584#.917ak4f6v (示例基于它)

关键词:茉莉+测试angular2。

于 2016-09-06T07:46:06.783 回答