【问题标题】:How to test map and tap pipe from RXJS in Angular如何在 Angular 中测试来自 RXJS 的地图和分接管道
【发布时间】:2020-06-14 20:22:01
【问题描述】:

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

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

我正在使用 Angular 9。

红线未经测试。

【问题讨论】:

  • 是的,您需要存根 HttpClient 调用,这在测试文档中有所介绍。然后,您可以为记录器创建一个间谍。从这样做开始。话虽如此,您的代码中不需要 map 运算符。
  • 您也可以查看使用angular.io/api/core/testing/fakeAsync 以确保您的异步工作在测试期间完成。

标签: angular unit-testing testing jasmine angular9


【解决方案1】:

是的,获得 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 在这两个函数中都没有做任何事情,它们可以被删除。他们只是返回他们收到的东西,并没有改变任何东西。

【讨论】:

  • 感谢您的回答和文章,感谢您关于不需要地图的说明,我没有意识到这一点。
猜你喜欢
  • 2019-10-16
  • 1970-01-01
  • 2019-09-13
  • 2020-09-05
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
相关资源
最近更新 更多