gpt4 book ai didi

Angular w/Jest : `verify()` vs .`expectOne()` ?

转载 作者:行者123 更新时间:2023-12-05 07:30:33 28 4
gpt4 key购买 nike

我有一个失败的测试,我不确定如何修复。我从 Jest 得到的错误消息似乎是矛盾的,问题与两个 Angular HttpTestingController 的行为有关。方法:verify()expectOne()

有问题的测试,在其文件的上下文中:

import {TestBed, getTestBed} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {PrintProjectsService} from './print-projects.service';
import {AppConfig} from '../../app.config';

describe('PrintProjectsService', () => {
let injector: TestBed;
let service: PrintProjectsService;
let appConfig: AppConfig;
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [PrintProjectsService, AppConfig]
});
injector = getTestBed();
service = injector.get(PrintProjectsService);
httpMock = injector.get(HttpTestingController);
appConfig = injector.get(AppConfig);
});

afterEach(() => {
httpMock.verify();
});

//This test passes
it('should make a GET request to retrieve a printable factory when provided a printable factory id', () => {
const id = '12345';
service.getPrintableFactory(id).subscribe();
const req = httpMock.expectOne(`${appConfig.API_URL}/api/printed-book/v1/printable-factories/${id}/`);
expect(req.request.method).toBe('GET');
});

// This is the one that fails
it('should make a GET request to retrieve cover image data from the cover service', () => {
const imageType = 'full';
service.getCoverImage(12345, '0850X1100FCSTDCO080CW444GXX', imageType).subscribe();
//httpMock.verify(); //this finds a GET at undefined/cover/api/cover-images/full
const req = httpMock.expectOne(`${appConfig.API_URL}/cover/api/cover-images/${imageType}`);
expect(req.request.responseType).toBe('blob');
});
});

Jest 返回此错误消息:

● PrintProjectsService › should make a GET request to retrieve cover image data from the cover service

Expected one matching request for criteria "Match URL: undefined/cover/api/cover-images/full", found none.

44 | service.getCoverImage(12345, '0850X1100FCSTDCO080CW444GXX', imageType).subscribe();
> 45 | const req = httpMock.expectOne(`${appConfig.API_URL}/cover/api/cover-images/${imageType}`);
46 | expect(req.request.responseType).toBe('blob');

at HttpClientTestingBackend.Object.<anonymous>.HttpClientTestingBackend.expectOne (node_modules/@angular/common/bundles/common-http-testing.umd.js:435:19)
at src/app/services/print-projects/print-projects.service.spec.ts:45:26
...
at Object.testBody.length (node_modules/jest-zone-patch/index.js:50:27)

● PrintProjectsService › should make a GET request to retrieve cover image data from the cover service

Expected no open requests, found 1: GET undefined/cover/api/cover-images/full

23 | afterEach(() => {
> 24 | httpMock.verify();
25 | });

错误消息中 URL 变量呈现为 undefined 的事实是无关紧要的 - 在通过的测试中也是如此。

令我困惑的是,当测试中到达expectOne()时,找不到undefined/cover/api/cover-images/full的请求,但在测试之后,verify()相同 URL 处发现了一个 GET 请求:undefined/cover/api/cover-images/fullverify() 还在 expectOne().

为什么 expectOne() 没有捕获到请求,而 verify() 却捕获了请求?错误消息没有告诉我我需要的一切吗?无论我运行 jest 还是 jest --verbose,我似乎都会收到相同的错误消息。

最佳答案

我找到了解决此问题的方法,使用从此处收集的 expectOne() 变体:https://github.com/thymikee/jest-preset-angular/blob/master/example/src/app/service/hero.service.spec.ts#L59

这是通过测试的新版本:

it('should make a GET request to retrieve cover image data from the cover service', () => {
const imageType = 'full';
service.getCoverImage(12345, '0850X1100FCSTDCO080CW444GXX', imageType).subscribe();
const req = httpMock.expectOne((request: HttpRequest<Blob>) => {
return request.url === `${appConfig.API_URL}/cover/api/cover-images/${imageType}`;
});
expect(req.request.method).toBe('GET');
// the original `expect()` below also passes, but since we already state that the request will return a Blob above, the `expect()` above is a better option
// expect(req.request.responseType).toBe('blob');
});

看起来好像 expectOne()url 只匹配版本,正如最初使用的那样,期望一个 JSON 响应默认。无论如何,这个特定调用的 Blob 响应类型似乎是问题的根源。

关于Angular w/Jest : `verify()` vs .`expectOne()` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52206486/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com