首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Angular中测试RXJS中的地图和攻丝管道

如何在Angular中测试RXJS中的地图和攻丝管道
EN

Stack Overflow用户
提问于 2020-03-02 22:06:35
回答 1查看 8.4K关注 0票数 2

我想测试下面的代码,但我不确定如何测试map和tap (从RXJS)函数。我是不是该做个模拟,用间谍?

我真的应该测试这些吗?我有一个习惯,那就是只实现这个目标就能获得100%的覆盖率(100%覆盖率),但我发现100%并不总是必要的,也不一定是有益的。但在这种情况下,map和tap对函数非常关键。我真的很感谢你的建议,谢谢。

我使用的是Angular 9。

红线是未经测试的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-02 22:19:56

是的,获得100%的覆盖率可能不是最好的想法,但却是一个很好的目标。

正如我所看到的,你的服务做超文本传输协议请求,遵循这个测试指南:https://medium.com/better-programming/testing-http-requests-in-angular-with-httpclienttestingmodule-3880ceac74cf

是的,你将不得不嘲笑loggingService

如下所示:

代码语言:javascript
复制
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在这两个函数中都没有执行任何操作,可以将它们删除。他们只是返回他们正在接收的东西,并没有改变任何东西。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60490532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档