error: No provider for @Attribute('sampleString')

error: No provider for @Attribute('sampleString')

我们正在尝试为使用第三方 java 脚本库的组件编写单元测试。我们组件的构造函数看起来像 -

@Constructor(@Inject(ElementRef) private eleref:ElementRef, @Attribute('sampleString') private sampleString: string)

我们使用该属性将其传递到我的第三方库。它在那里根据该属性执行特定任务。如果我没有通过它,就意味着忽略它并做常规的事情。

当我们在测试 class 中尝试 use/inject 这个组件时,它给了我们错误。

Error: DI Exception: No Provider for @Attribute('sampleString')!

有人可以建议这方面的提供商吗?如果您的示例可以详细说明为什么会出现此错误以及一般如何解决此类问题,那将是加分项。

//component
@component({selector: 'mytable', templateUrl:'URL of template'}
export class mycomp{ 
//data members
constructor (element ref injection, @Attribute('sample string') private sampleString:string){}
//public methods
private ngOninit(){ this.dataview = dataview of third party lib.    }
}

//Test
Describe("my test",()=>{ 
beforeEachProviders(()=>[ mycomp, provider for elementRef]);

It('test', async(inject ([TestComponentBuilder,mycomp], (tcb: TestComponentBuilder) => {
tcb.createAsync(mycomp)
.then ((fixture)=> {
expect(true). toBe(false)
})
 });

属性需要是

@Attribute('sampleString')

而不是

@Attribute('sampleString')

您需要一个测试组件来包装您实际要测试的组件才能传递属性:

@component({
  selector: 'mytable', 
  templateUrl:'URL of template'
}
export class mycomp{ 
  //data members
  constructor (element ref injection, @Attribute('sampleString') private   sampleString:string){}
  //public methods
  private ngOninit(){ this.dataview = dataview of third party lib.    }
}

@component({
  selector: 'test', 
  template:'<mytable sampleString="xxx"></mytable>'
}
export class TestComponent{ 
}

//Test
describe("my test",()=>{ 
  beforeEachProviders(()=>[ mycomp, provider for elementRef]);

it('test', async(inject ([TestComponentBuilder,mycomp], (tcb: TestComponentBuilder) => {
  tcb.createAsync(TestComponent)
 .then ((fixture)=> {
    expect(true). toBe(false)
    // get the mycomp component from fixture ...
  })
});