Test pipe with dependencies on services

前端 未结 3 1713
陌清茗
陌清茗 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:23

    just in case anyone would like to reuse the constructor of the Pipe, you can use the TestBed to acheive the same result :

      let pipe: SafeHtmlPipe;
      let sanitized: DomSanitizer
    
      beforeEach(async() => {
        TestBed.configureTestingModule({
          providers: [DomSanitizer]
        });
        sanitized = TestBed.get(DomSanitizer);
        pipe = new SafeHtmlPipe(sanitized);
      });
    
      it('create an instance', () => {
        expect(pipe).toBeTruthy();
      });
    

提交回复
热议问题