簡體   English   中英

Angular2在裝飾器中測試數據

[英]Angular2 testing data in decorators

我有一個組件,我想在單元測試中檢查選擇器

@Component({
     selector: 'my-component',
)}

我想這樣測試

describe('My Component', function(){ 
   it('should have a selector', function() {
      expect( ___ ).toBe('my-component');
   }); 
)}

您需要使用Reflect Metadata來訪問這些提示。 假設組件的類是MyComponent ,您可以這樣做來測試選擇器:

describe('My Component', function(){ 
  it('should have a selector', function() {
    var annotations = Reflect.getMetadata('annotations', MyComponent);
    // Supposing the first annotations is of type ComponentMetadata
    expect(annotations[0].selector).toBe('my-component');
  });
});

請參閱此plunkr: https ://plnkr.co/edit/HQc1qt p = preview。

在rc上,您可能需要從核心檢索重新折射器,然后使用reflector.annotations來檢索信息

import {reflector} from '@angular/core/src/reflection/reflection';

it('should have a selector', inject([SettingsComponent], (component: any) => {
    let meta = reflector.annotations(SettingsComponent)[0];
    expect(meta.selector).toBe('Settings');
}));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM