Angular2在装饰器中测试数据

Mar*_*thy 4 jasmine angular

我有一个组件,我想在单元测试中检查选择器

@Component({
     selector: 'my-component',
)}
Run Code Online (Sandbox Code Playgroud)

我想这样测试

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

Thi*_*ier 6

您需要使用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');
  });
});
Run Code Online (Sandbox Code Playgroud)

请参阅此plunkr:https://plnkr.co/edit/HQc1qt p = preview .