2016-11-04 6 views
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...} 
}); 

私は原料の束を試してみましたが、どこでも検索が、前にこれをしなかった人を見つけることができませんでしたしました。

ありがとうございます!

フィリップ

+0

の順序ではないということを証明しましたあなたがこれをやっている理由を見てください...しかし私にとっては、あなたのparentInjectorがコンポーネントを作成するようです。したがって、コンポーネント自体もparentInjectorを使用し、parentInjectorはinjectInputを認識しません。たぶんあなたはあなたの意図を記述してください、そして、おそらく、それを解決する別の方法が、注入ロジック全体を再配線するよりも –

答えて

0

が;-)

@Component({ 
    selector: 'my-component', 
    template: '' 
}) 
export class TestComponent { 
    constructor(
    private injector : Injector 
) { 
    injector.get('value1'); 
    } 
} 

describe('a test',() => { 
    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ TestComponent ], 
     providers: [ TestComponent ] 
    }); 
    }); 

    beforeEach(() => { 
    this.injector1 = ReflectiveInjector.resolveAndCreate([ 
     {provide: 'value1', useValue: 5} 
    ]); 

    }); 

    it('inject value',() => { 
    expect(this.injector1.get('value1')).toBe(5); 
    }); 

    describe('create component',() => { 
    it('with untouched injector should throw error',() => { 
     expect(() => TestBed.createComponent(TestComponent)).toThrowError(); 
    }) 

    it('with manipulated injector',() => { 
     let componentInjector = TestBed.get(Injector); 
     componentInjector.parent = this.injector1; 
     expect(TestBed.createComponent(TestComponent)).toBeTruthy(); 
    }) 

    it('with injectors in the wrong order',() => { 
     let componentInjector = TestBed.get(Injector); 
     let combinedInjector = ReflectiveInjector.fromResolvedProviders(this.injector1, componentInjector); 
     expect(() => combinedInjector.get(TestComponent)).toThrowError(); 
    }) 
    }); 
}); 

http://plnkr.co/edit/PlUUtTOZq8bPLQ5WdAbE?p=preview

を、plnkrでビットを実験例として、あなたの問題を使用するには、それを行うインジェクタ

関連する問題