gpt4 book ai didi

angular - 如何在 Angular 中测试来自 RXJS 的 map 和水龙头管道

转载 作者:行者123 更新时间:2023-12-04 10:29:09 25 4
gpt4 key购买 nike

我想在下面测试我的代码,但我不确定如何测试 map 和点击(来自 RXJS)功能。我应该做一个模拟,使用 spy 吗?

我真的应该测试这些吗?我有一个习惯,即为了实现该目标(100% 的覆盖率)而获得 100% 的覆盖率,但我了解到 100% 并不总是需要或有益的。但在这种情况下,map 和 tap 对功能来说非常重要。我非常感谢任何建议,谢谢。

我正在使用 Angular 9。

红线是未经测试的。

enter image description here

最佳答案

是的,获得 100% 的覆盖率可能不是最好的主意,但却是一个不错的目标。

我看到您的服务执行 HTTP 请求,请按照本指南进行测试:https://medium.com/better-programming/testing-http-requests-in-angular-with-httpclienttestingmodule-3880ceac74cf .

是的,您必须模拟 loggingService

像这样:

import { TestBed } from '@angular/core/testing';
import { CoursesService } from './courses.service';
import { HttpClientTestingModule,
HttpTestingController } from '@angular/common/http/testing';
... // the rest of your imports

describe('TemplateService', () => {
// We declare the variables that we'll use for the Test Controller and for our Service
let httpTestingController: HttpTestingController;
let service: TemplateService;
let mockLoggingService: any;

beforeEach(() => {
// 'loggingService' is for your own sanity and the array are all of the public methods of `loggingService`
mockLoggingService = jasmine.createSpyObj('loggingService', ['logger']);
TestBed.configureTestingModule({
providers: [
TemplateService,
// every time the test asks for LoggingService, supply this mock
{ provide: LoggingService, useValue: mockLoggingService },
],
imports: [HttpClientTestingModule]
});

// We inject our service (which imports the HttpClient) and the Test Controller
httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(TemplateService);
});

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

// Angular default test added when you generate a service using the CLI
it('should be created', () => {
expect(service).toBeTruthy();
});

it('should make a get call for getTemplates and log', () => {
const mockTemplates: Template[] = [/* you know how a template should look like so mock it*/];
// make HTTP call take flight
service.getTemplates().subscribe(templates => {
expect(templates).toEqual(mockTemplates);
expect(mockLoggingService.logger).toHaveBeenCalledWith(`User viewed templates: ${templates}`);
});

// have a handle on the HTTP call that is about to take flight
const req = httpTestingController.expectOne(/* put the unique url of this method's get request that only you know of */);

expect(req.request.method).toEqual('GET');
// send this request for the next HTTP call
req.flush(mockTemplates);
});

it('should make a get call for getTemplateById and log', () => {
const mockTemplate: Template = {/* you know how a template should look like so mock it*/};
// make HTTP call take flight
service.getTemplateById(1).subscribe(template => {
expect(template).toEqual(mockTemplate);
expect(mockLoggingService.logger).toHaveBeenCalledWith(`User viewed template: ${template}`);
});

// have a handle on the HTTP call that is about to take flight
const req = httpTestingController.expectOne(/* put the unique url of this method's get request that only you know of */);

expect(req.request.method).toEqual('GET');
// send this request for the next HTTP call
req.flush(mockTemplates);
});
});

旁注:您的 maps 在这两个函数中都没有做任何事情,它们可以被删除。他们只是返回他们收到的东西,并没有改变任何东西。

关于angular - 如何在 Angular 中测试来自 RXJS 的 map 和水龙头管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490532/

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