Test pipe with dependencies on services

前端 未结 3 1672
陌清茗
陌清茗 2021-02-02 08:41

I have a pipe that sanatises HTML as below:

import { Pipe, PipeTransform } from \'@angular/core\';
import { DomSanitizer } from \'@angular/platform-browser\';

@         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 09:22

    Because of the DI in your pipe, you need to configure a test environment (test bed) to resolve the dependency:

    import { BrowserModule, DomSanitizer } from '@angular/platform-browser';
    import { inject, TestBed } from '@angular/core/testing';
    
    describe('SanitiseHtmlPipe', () => {
      beforeEach(() => {
        TestBed
          .configureTestingModule({
            imports: [
              BrowserModule
            ]
          });
      });
    
      it('create an instance', inject([DomSanitizer], (domSanitizer: DomSanitizer) => {
        let pipe = new SanitiseHtmlPipe(domSanitizer);
        expect(pipe).toBeTruthy();
      })); 
    });
    

提交回复
热议问题