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

拟墨画扇 提交于 2019-12-02 06:26:16

问题


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)
})
 });

回答1:


The attribute needs to be

@Attribute('sampleString')

instead of

@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 ...
  })
});


来源:https://stackoverflow.com/questions/38448301/error-no-provider-for-attributesamplestring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!