Angular unit testing "SPEC HAS NO EXPECTATIONS" and "error: Timeout"

Angular unit testing "SPEC HAS NO EXPECTATIONS" and "error: Timeout"

我正在尝试为我的 post 函数编写测试。这是一个 Angular 应用程序,我正在使用 Karma/Jasmine 进行测试。

这是我的测试:

 it(`should post data`, ()=>{
    const service: ApiService = TestBed.get(ApiService);
    service.sendMessage(data).subscribe(values =>{
      expect(values).toBe('observable value');
    });
  });

这两种方法都“通过”了测试,但都有“SPEC HAS NO EXPECTATIONS”消息。我为此找到的修复方法是尝试从中获得承诺。我尝试了两种方法:

添加“完成()”

 it(`should post data`, (done:any)=>{
    const service: ApiService = TestBed.get(ApiService);
    service.sendMessage(data).subscribe(values =>{
      expect(values).toBe('observable value');
      done();
    });
  });

或将整个内容重写为:

it('should post data', inject([ApiService], async (service: ApiService) => {
    const tada = await service.sendMessage(data).toPromise();
    expect(tada).toBe('observable data');
  }));

这些调用测试由于超时而失败。(错误:超时 - 异步功能未在 5000 毫秒内完成(由 jasmine.DEFAULT_TIMEOUT_INTERVAL 设置))因此,为了解决这个问题,我尝试增加超时时间。我用了

jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;

但即使经过更长的时间,它仍然会超时。这让我觉得这不是正确的解决方案,但我找不到超出我尝试过的答案。我也试过移动部分,比如将 expect() 放在订阅之外,等等。

编辑:这是整个单元测试文件

import { TestBed } from '@angular/core/testing';
import { ApiService } from './api.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';

describe('ApiService', () => {
  let service: ApiService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [ApiService]
    });
    // service = TestBed.inject(ApiService);
  });

  it('should be created', () => {
    const service: ApiService = TestBed.get(ApiService);
    expect(service).toBeTruthy();
  });

  let data = {
    name: "fdsgs",
    sex: "gfjhghj",
    address:"ghmfgj",
    county: "ghjdgfhj",
    contactMethod: ["dfghjsfdghd", "helloworld"],
    daysAvailable: ["dfghfdgh", "heloo world"],
    timesAvailable: ["sdfghfgh", "hello world"],
    aboutPerson: ["stfghfdghd", "sadf"],
    aboutChildren: ["jdfhjtd", "awert"],
    questions: "gfhdfgh",
    howTheyHeardOfUs: ["dfghasg", "fasfg"]
  }

  it(`should post data`, (done: DoneFn)=>{
    const service: ApiService = TestBed.get(ApiService);
    service.sendMessage(data).subscribe(values =>{
      expect(values).toBe('observable value');
      done();
    });
  });

这里是 API 服务文件:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';

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

  constructor(private http: HttpClient) { }

  sendMessage(data:any){
    return this.http.post<any>("URL", data)
    .pipe(map((res:any)=>{
      return res;
    }));
  }
}

我不会碰 jasmine.DEFAULT_TIMEOUT_INTERVAL 属性 因为我认为默认的 5 秒对于单元测试来说足够了。

这个方法不错:

it(`should post data`, (done: DoneFn)=>{
    const service: ApiService = TestBed.get(ApiService);
    // !! service.sendMessage is never emitting and therefore never going
    const httpTestingController = TestBed.get(HttpTestingController);
    // inside of the subscribe block !!
    service.sendMessage(data).subscribe(values =>{
      expect(values).toEqual({});
      done();
    });
    
    // expect an http call
    const req = httpTestingController.expectOne(request => request.method === 'POST');
    // flush an empty object for it
    req.flush({});
  });

问题在单元测试文件中作为注释写入。

如果您能向我展示服务和整个单元测试文件,我可能会提供帮助。问题是从 sendMessage 返回的 observable 永远不会发射。

在服务中也去掉这个:

.pipe(map((res:any)=>{
      return res;
    })

它什么都不做。