简体   繁体   English

具有 DomSanitizer 依赖项的 Angular 6 单元测试组件

[英]Angular 6 unit testing component with DomSanitizer dependency

In a unit test to just create (instantiate) a component that has a DomSanitizer dependency, how does one mock / stub this dependency?在仅创建(实例化)具有DomSanitizer依赖项的组件的单元测试中,如何模拟/存根此依赖项?

Because DomSanitizer is an Abstract class, I have no idea what the method signature of bypassSecurityTrustHtml really looks like.因为DomSanitizer是一个Abstract类,我不知道bypassSecurityTrustHtml的方法签名bypassSecurityTrustHtml是什么样子。

And if it's not intended to mock / stub DomSanitizer , how should one proceed to inject the actual implementation iso the abstract class?如果它不打算模拟/存根DomSanitizer ,那么应该如何继续注入抽象类的实际实现 iso ?

actual statement in the component looks like:组件中的实际语句如下所示:

this.trustedString = <string>this.domSanitizer.bypassSecurityTrustHtml(trustedHTML);

TestBed setup looks like: TestBed设置如下所示:

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      BrowserModule,
      // other modules
    ],
    providers: [
      {
        provide: DomSanitizer,
        useValue: {
          bypassSecurityTrustHtml: () => 'safeString'
        }
      },
      // more providers
    ],
    declarations: [ TheComponent ],
    schemas: [ NO_ERRORS_SCHEMA ]
  })
    .compileComponents();
}));

The specific error that I'm getting in Karma in Chrome (not headless) is this:我在 Chrome 中的 Karma(非无头)中遇到的具体错误是:

TypeError: view.root.sanitizer.sanitize is not a function

error properties: Object({ ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 16793601, rootNodeFlags: 1, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 16793601, directChildFlags: 16777217, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references: Object({ }), ngContentIndex: null, childCount: 5, bindings: [ ], bindingFlags: 0, outputs: [ ], element: Object({ ns: null, name: null, attrs: [ ], template: null, componentProvider: null, componentView: null, componentRendererType: null, publicProviders: null({ }), allProviders: null({ }), handleEvent: Function }), provider: null, text: null, query: null, ngContent: null }), Object({ nodeIndex: 1, parent: Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 16793601, directChildFlag错误属性:对象({ ngDebugContext:DebugContext_({视图:对象({def:对象({工厂:函数,nodeFlags:16793601,rootNodeFlags:1,nodeMatchedQueries:0,标志:0,节点:[对象({nodeIndex:0 ,parent:null,renderParent:null,bindingIndex:0,outputIndex:0,checkIndex:0,flags:1,childFlags:16793601,directChildFlags:16777217,childMatchedQueries:0,matchedQueries:Object({}),matchedQueryIds:0,引用: Object({ }), ngContentIndex: null, childCount: 5, bindings: [ ], bindingFlags: 0, 输出: [ ], element: Object({ ns: null, name: null, attrs: [ ], template: null , componentProvider: null, componentView: null, componentRendererType: null, publicProviders: null({ }), allProviders: null({ }), handleEvent: Function }), provider: null, text: null, query: null, ngContent: null }), Object({ nodeIndex: 1, parent: Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 16793601, directChildFlag s: 16777217 ... at at setElementProperty (webpack:///./node_modules/@angular/core/fesm5/core.js?:8237:61) at checkAndUpdateElementValue (webpack:///./node_modules/@angular/core/fesm5/core.js?:8189:13) at checkAndUpdateElementInline (webpack:///./node_modules/@angular/core/fesm5/core.js?:8136:24) at checkAndUpdateNodeInline (webpack:///./node_modules/@angular/core/fesm5/core.js?:10477:20) at checkAndUpdateNode (webpack:///./node_modules/@angular/core/fesm5/core.js?:10443:16) at debugCheckAndUpdateNode (webpack:///./node_modules/@angular/core/fesm5/core.js?:11076:38) at debugCheckRenderNodeFn (webpack:///./node_modules/@angular/core/fesm5/core.js?:11062:13) at Object.eval [as updateRenderer] (ng:///DynamicTestModule/ConversationMessageComponent.ngfactory.js:84:5) at Object.debugUpdateRenderer [as updateRenderer] (webpack:///./node_modules/@angular/core/fesm5/core.js?:11054:21) at checkAndUpdateView (webpack:///./node_modules/@angular/core/fesm5/core.js?:10430:14) s: 16777217 ... 在 setElementProperty (webpack:///./node_modules/@angular/core/fesm5/core.js?:8237:61) 在 checkAndUpdateElementValue (webpack:///./node_modules/@angular/ core/fesm5/core.js?:8189:13) 在 checkAndUpdateElementInline (webpack:///./node_modules/@angular/core/fesm5/core.js?:8136:24) 在 checkAndUpdateNodeInline (webpack:///. /node_modules/@angular/core/fesm5/core.js?:10477:20) 在 checkAndUpdateNode (webpack:///./node_modules/@angular/core/fesm5/core.js?:10443:16) 在 debugCheckAndUpdateNode ( webpack:///./node_modules/@angular/core/fesm5/core.js?:11076:38) 在 debugCheckRenderNodeFn (webpack:///./node_modules/@angular/core/fesm5/core.js?:11062) :13) 在 Object.eval [as updateRenderer] (ng:///DynamicTestModule/ConversationMessageComponent.ngfactory.js:84:5) 在 Object.debugUpdateRenderer [as updateRenderer] (webpack:///./node_modules/@angular/ core/fesm5/core.js?:11054:21) 在 checkAndUpdateView (webpack:///./node_modules/@angular/core/fesm5/core.js?:10430:14)

As a workaround, try add sanitize: () => 'safeString',作为一种解决方法,尝试添加sanitize: () => 'safeString',

...
useValue: {
  sanitize: () => 'safeString',
  bypassSecurityTrustHtml: () => 'safeString'
}
...

If you want to preserve the value, use:如果要保留该值,请使用:

{
  provide: DomSanitizer,
  useValue: {
    sanitize: (ctx: any, val: string) => val,
    bypassSecurityTrustResourceUrl: (val: string) => val,
  },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM