Angular:测试ds.bypassSecurityTrustResourceUrl期间发生错误不是函数

时间:2019-09-05 06:59:01

标签: angular unit-testing jasmine

我是一名有经验的新手,在测试过程中提出了一个问题,我在演示中引用了svg资源,程序可以正常运行,但测试未达到预期。

spec.ts

import { TestBed, async } from '@angular/core/testing';
import { MatIconRegistry } from "@angular/material";
import { DomSanitizer,BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from "@angular/common/http";

import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { RouterLinkDirectiveStub } from "./components/dashboard/dashboard.component.spec";

fdescribe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        BrowserAnimationsModule,
        BrowserModule,
        HttpClientModule
      ],
      declarations: [
        AppComponent,
        RouterLinkDirectiveStub,
      ],
      providers:[
        MatIconRegistry,
        DomSanitizer,
      ],
      schemas: [ NO_ERRORS_SCHEMA ]
    }).compileComponents();
    TestBed.get(MatIconRegistry);
    TestBed.get(DomSanitizer);
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });

  it(`should have as title 'angular-heros'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('angular-heros');
  });

  it('should render title in a h1 tag', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to angular-heros!');
  });
});

component.ts

export class AppComponent {
  constructor(ir:MatIconRegistry,ds:DomSanitizer){
    // src\assets\svgIcons\play-circle.svg
    const svgIcons='assets';
    ir.addSvgIcon('aa',ds.bypassSecurityTrustResourceUrl(`${svgIcons}/sharp.svg`))
    // ir.addSvgIconInNamespace('custom-svg','angular',ds.bypassSecurityTrustResourceUrl('assets/imges/angular.svg'));

  }
  title = '指南';
}

我希望测试能够通过。 测试“ TypeError:ds.bypassSecurityTrustResourceUrl不是函数”时抛出

0 个答案:

没有答案