Angular 角度服务方法的单元测试用例

Angular 角度服务方法的单元测试用例,angular,typescript,unit-testing,Angular,Typescript,Unit Testing,我想为服务类编写测试用例 我的服务类dynamic-form.Service.ts import { HttpClient, HttpBackend } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; @Injectable({ providedIn: 'root' }) export class Dynami

我想为服务类编写测试用例

我的服务类dynamic-form.Service.ts

import { HttpClient, HttpBackend } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class DynamicFormService {

  private http: HttpClient;

  constructor(private handler: HttpBackend
  ) {
    this.http = new HttpClient(handler);
  }

  async getDynamicFormData(paramName: any, data?: any): Promise<any> {
    return await this.http.get(this.HostUrl + paramName).toPromise();
  }

  async getTaskByWorkflowId(paramNameWithId): Promise<any> {
    return await this.http.get(this.HostUrl + paramNameWithId).toPromise();
  }

  async getAccontType(paramNameWithId): Promise<any> {
    return await this.http.get(this.HostUrl + paramNameWithId).toPromise();
  }
}
我不知道如何从编写单元测试用例开始。我尝试在谷歌上搜索一些例子,并尝试使用spyOn实现,但它给了我一些错误。 任何测试我的服务方法的正确方法

目前我写了一个测试用例,比如

it('should call getAccontType', ()=>{
    const data = [
      {
        "id": 4,
        "name": "24hrs"
      },
      {
        "id": 5,
        "name": "48hrs"
      }];
    service.getAccontType(`account-type`).then(res=> expect(res.length).toBeGreaterThan(0));
    const request = httpMock.expectOne(`http://localhost:8080/account-type`);
    expect(request.request.method).toBe('GET');
    request.flush(data);
    httpMock.verify();
  });

请提供出现
spyOn
故障的代码,以便我们可以帮助您,否则stackoverflow不是教程推荐的地方。@Б砦砦砦砦砦砦砦砦砦砦砦砦30758。我没有使用spyOn,而是更改了实现。这是不是一个好办法。我没有使用spyOn时遇到的错误详细信息。我认为您的测试名称错误,您的测试正在检查
getAccountType
是否返回响应,而不是它是否已被调用。你可以把你的问题贴在这里,我想会有人给你提供充分的反馈
it('should call getAccontType', ()=>{
    const data = [
      {
        "id": 4,
        "name": "24hrs"
      },
      {
        "id": 5,
        "name": "48hrs"
      }];
    service.getAccontType(`account-type`).then(res=> expect(res.length).toBeGreaterThan(0));
    const request = httpMock.expectOne(`http://localhost:8080/account-type`);
    expect(request.request.method).toBe('GET');
    request.flush(data);
    httpMock.verify();
  });