angular - 在 HTTP 请求中测试 RxJS 映射函数
问题描述
我正在测试一个包含私有映射器方法的 Angular 服务,如下所示,我的目标是在 RxJS 映射中模拟该方法,因为它似乎没有经过覆盖测试。
这是我的 Jest 测试,但该方法似乎没有经过测试,也许我需要模拟私有方法?
test('should test the download file', () => {
const fakeurl = 'http://fakeurl';
service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(resp).toBe(mockDownloadedFile);
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
});
解决方案
我在想,因为delay
你的断言在你subscribe
永远不会运行。
试试这个以确保订阅内部的断言正在运行
test('should test the download file', () => {
const fakeurl = 'http://fakeurl';
service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(1).toBe(2); // this test should fail
expect(resp).toBe(mockDownloadedFile);
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
subscripton.unsubscribe(); // unusbscribe from previous subscription
});
确保上述测试未能确保它实际上进入了断言的订阅块内部。如果它通过了,你就知道它没有进入订阅块。
对于您的情况,我会利用fakeAsync
虚假的方式提前时间:
import { fakeAsync, tick } from "@angular/core/testing";
....
test('should test the download file', fakeAsync(() => { // add fakeAsync here
const fakeurl = 'http://fakeurl';
let response: IDownload;
const subscription = service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(1).toBe(2); // ensure this fails and remove it once you see it failing
response = resp; // assign response here
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
tick(3500); // advance the timer by 3500ms so it gets passed the delay
expect(response).toBe(mockDownloadedFile);
}));
现在我想既然你在使用jest
,你会遇到使用问题fakeAsync
。也许将其更改test
为 an it
,看看这是否对您有用。
推荐阅读
- python - typing.NamedTuple 和可变的默认参数
- android - 如何在应用程序中的实时位置仅添加 1 个标记
- php - 如何使它在 PHP 中读取值而不是名称
- javascript - 如何在mongodb的创建和删除过程中确保最小值?
- javascript - 在 Linux 服务器的节点 js 中守护任务
- python-3.x - BitlyApi:错误 500 (RATE_LIMIT_EXCEEDED)
- typescript - 与对象接口,但具有特定值
- verilog - 系统 Verilog 测试台回归运行
- spring-boot - 如何在 Spring Boot 启动时禁用连接到数据库
- sql-server-2008 - SQL server 数据表在 VS2019 Blazor WSAM 中不可见