【问题标题】:How to test services in angular using jasmine如何使用 jasmine 在 Angular 中测试服务
【发布时间】:2021-03-22 13:30:54
【问题描述】:

我是使用 jasmine 在 Angular 中进行单元测试的初学者。如何使用 jasmine 测试服务? 以下是一项服务:

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));
  }
}

如何在规范文件中编写测试用例来测试此服务中存在的方法?请帮忙。

【问题讨论】:

    标签: angular unit-testing jasmine karma-jasmine


    【解决方案1】:

    请参考我不久前回答的这篇帖子。

    Test value inside finalize pipe in angular test

    您需要在您的规范文件中创建一个模拟服务并使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2017-08-04
      相关资源
      最近更新 更多