我们正在尝试为使用第三方java脚本库的组件编写单元测试。我们组件的构造函数看起来像 -
@Constructor(@Inject(ElementRef) private eleref:ElementRef, @Attribute('sampleString') private sampleString: string)
我们使用该属性将其传递给我的第三方库。在那里,它根据该属性执行特定任务。如果我没有通过它,则意味着完全忽略它并做常规的事情。
当我们尝试在我们的测试类中使用/注入此组件时,它会给我们带来错误。
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)
})
});
答案 0 :(得分:2)
属性需要
@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 ...
})
});