2016-07-19 14 views
0

angular2ユニットテストから始まり、問題が発生しています。 私はそのパラメータApplicationRefComponentResolver サービスは、次に、動的コンポーネントをロードするComponentResolverを使用として受け入れるサービスを有しています。 私の質問は、上記のパラメータをそのサービスに単体テストで渡すにはどうすればいいですか?サービス(依存関係あり)angular2のユニットテスト

+0

?あなたは何をしているのかを示すコードを提供してください。 https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02 'setBaseTestProviders'が引き続き使用できるかどうかはわかりません。 –

答えて

0

この例では、あなたを助けています:

何Angular2バージョン
import {Component, provide} from '@angular/core'; 
import {ComponentFactory} from '@angular/core/src/linker/component_factory'; 
import {ComponentResolver, ReflectorComponentResolver} from '@angular/core/src/linker/component_resolver'; 
import {ReflectionInfo, reflector} from '@angular/core/src/reflection/reflection'; 
import {afterEach, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; 
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; 
import {Console} from '../../src/console'; 

class DummyConsole implements Console { 
    log(message: string) {} 
    warn(message: string) {} 
} 

export function main() { 
    describe('Compiler',() => { 
    var someCompFactory: any /** TODO #9100 */; 
    var compiler: ComponentResolver; 

    beforeEach(() => { 
     someCompFactory = new ComponentFactory(null, null, null); 
     reflector.registerType(SomeComponent, new ReflectionInfo([someCompFactory])); 
     compiler = new ReflectorComponentResolver(new DummyConsole()); 
    }); 

    it('should read the template from an annotation', 
     inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { 
     compiler.resolveComponent(SomeComponent).then((compFactory: ComponentFactory<any>) => { 
      expect(compFactory).toBe(someCompFactory); 
      async.done(); 
      return null; 
     }); 
     })); 

    it('should throw when given a string', 
     inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { 
     compiler.resolveComponent('someString').catch((e) => { 
      expect(e.message).toContain('Cannot resolve component using \'someString\'.') 
       async.done(); 
     }); 
     })); 
    }); 
} 

class SomeComponent {} 
関連する問題