6
私はComponentFactoryResolverを使用してコンポーネントを動的に作成し、ReflectiveInjectorを使用して動的に入力を渡します。Angular2 2.0.1コアインジェクタを使用したコンポーネントユニットテスト
これは次に、動的に作成されたコンポーネントは、私はコアインジェクタモジュールを使用して、そのコンポーネントのユニットテストを書くしようとしているこの
import {Component, Injector} from '@angular/core';
@Component({
selector: 'mycomponent'
})
export class MyComponent {
private id: string;
constructor(private injector: Injector) {
this.id = injector.get('injectedInput');
}
}
のように見えます
@ViewChild('container', {read: ViewContainerRef}) container: ViewContainerRef;
let inputProviders = [{
provide: 'injectedInput',
useValue: inputValue
}];
let resolvedInputs = ReflectiveInjector.resolve(inputProviders);
let injector = ReflectiveInjector.fromResolvedProviders(resolvedInputs, this.container.parentInjector);
let factory = componentInfo.factory.resolveComponentFactory(componentInfo.component);
let component = factory.create(injector);
this.container.insert(component.hostView);
よう程度になります。私は次のエラーを取得しています:
Error: No provider for injectedInput!
マイspecファイルは次のようになります。
import { MyComponent } from 'here';
describe('MyComponent',() => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
MyComponent
]
});
});
let component: MyComponent;
beforeEach(inject([RigTransferSpeedPeriodComponent], _component => {
component = _component;
}));
{...my tests...}
});
私は原料の束を試してみましたが、どこでも検索が、前にこれをしなかった人を見つけることができませんでしたしました。
ありがとうございます!
フィリップ
の順序ではないということを証明しましたあなたがこれをやっている理由を見てください...しかし私にとっては、あなたのparentInjectorがコンポーネントを作成するようです。したがって、コンポーネント自体もparentInjectorを使用し、parentInjectorはinjectInputを認識しません。たぶんあなたはあなたの意図を記述してください、そして、おそらく、それを解決する別の方法が、注入ロジック全体を再配線するよりも –