0

I am a beginner in unit testing in Angular using jasmine. How do I test a service using jasmine? Below is a service:

import { Injectable } from "@angular/core";
import { environment } from "src/environments/environment";
import { MessageService } from "../message/message.service";
import { Observable } from "rxjs";
import { catchError } from "rxjs/operators";
import { ShareDto } from "src/app/model/share/share-dto";
import { HttpHandlerService } from "../http-handler/http-handler.service";
 
@Injectable({
  providedIn: "root"
})
export class xxxService {
  private apiUrl = environment.requestUrl;
  private shareDefaultUrl = "/share";
 
  constructor(
    private http: HttpHandlerService,
    private messageService: MessageService
  ) {}
 
  sharing(body: ShareDto): Observable<any> {
    const url = `${this.shareDefaultUrl}`;
 
    return this.http
      .post(this.apiUrl + url, body)
      .pipe(catchError(this.messageService.handleError));
  }
}

How do I write the testcase in spec file to test the methods present in this service? Please help.

Ankit Choudhary
  • 145
  • 2
  • 13

1 Answers1

0

Please refer to this post that I answered a while back.

Test value inside finalize pipe in angular test

You need to create a mock Service in your spec file and use it.

Srikar Phani Kumar M
  • 1,069
  • 1
  • 3
  • 8