问题描述
我们正在尝试为使用第三方Java脚本库的组件编写单元测试.我们组件的构造函数看起来像-
We are trying to write unit test for a component which uses a third party java script library. The constructor of our component looks like -
@Constructor(@Inject(ElementRef) private eleref:ElementRef, @Attribute('sampleString') private sampleString: string)
我们使用该属性将其传递给我的第三方库.并根据该属性执行特定任务.如果我不通过,那就意味着只需忽略它,然后做常规的事情.
We use that attribute to pass it to my third party library. And in there it does a specific task based on that attribute. If I don't pass it, it means simply ignore it and do regular stuff.
当我们尝试在测试类中使用/注入该组件时,会给我们带来错误.
When we try to use/inject this component in our test class, it gives us error.
Error: DI Exception: No Provider for @Attribute('sampleString')!
有人可以建议提供什么吗?如果您的示例可以详细说明为什么会出现此错误以及通常如何解决此类问题,那么这将是有好处的.
Can someone suggest for what would be the provider for this? If your example can elaborate why this error and how to solve such issues in general, that will be bonus.
//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')
您需要一个测试组件,该组件包装您实际要测试的组件以能够传递属性:
You need a test component that wraps the component that you actually want to test to be able to pass the attribute:
@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 ...
})
});
这篇关于错误:@Attribute('sampleString')没有提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!