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

标签 angular unit-testing testing jasmine angular9

我想在下面测试我的代码,但我不确定如何测试 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/

相关文章:

angular - 取消路由解析

javascript - 来自 Asp.Net Core 的 Angular 4 映射结果

用于浏览器对象和 HTML DOM 对象的 JavaScript 模拟库

ruby-on-rails - Controller 测试错误消息 : "ActionView::Template::Error: The asset "MyString"is not present in the asset pipeline"

unit-testing - 为什么在使用 Jest 运行测试时,Trix 编辑器不会挂载到 Vue 组件中?

javascript - 我可以从 onprepare 中的函数传递参数来覆盖 Protractor 配置文件吗

ruby-on-rails - 如何测试 API 错误?

angular - 可以使用 Ionic 2 更改手机和平板电脑的布局吗?

c++ - 访问实例类的变量

Angular2 + RxJS - 无法读取未定义的下一个属性