我有一个组件,我想在单元测试中检查选择器。
@Component({
selector: 'my-component',
)}我想像这样测试
describe('My Component', function(){
it('should have a selector', function() {
expect( ___ ).toBe('my-component');
});
)}发布于 2016-03-17 07:41:31
在您需要使用反射元数据来访问这些提示。假设组件的类是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。
发布于 2016-08-12 14:01:12
在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');
}));https://stackoverflow.com/questions/36053166
复制相似问题