vendredi 1 juin 2018

Best way to unit-test createProduct service using Angular 5 and Jasmine 2.6.4

I need to test service create new product. So, I have this service code:

  public CreateProduct(newProd: Product): Observable<boolean> {
    .....
    let body = newProd.generateUrlencodedParameters(this.currentuser().token);
    return this.http.post(API.getUrl(Api.URLS.CreateProduct), body, {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();

        if (res.StatusCode === 0) {
          return true;
        }
    }

Class Product:

export class Product{
    id: number;
    prod_number: String;
    prod_name: string;

    constructor(obj: any) {
        this.id= obj.id;
        this.prod_number= obj.prod_number;
        this.prod_name= obj.prod_name;
    }
    public generateUrlencodedParameters(token: string, id?: number): string {
        let urlSearchParams = new URLSearchParams();
        urlSearchParams.append('prod_number', this.prod_number.toString());
        urlSearchParams.append('prod_name', this.prod_name.toString());
        urlSearchParams.append('token', token);
        return urlSearchParams.toString();
    }
}

What is best way to test this service?

Aucun commentaire:

Enregistrer un commentaire