[英]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');
});
});
在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.