2017-03-05 115 views
2

我似乎無法在角2測試牀上註冊多個提供商(服務)。我只想給測試平臺提供多種服務。這裏是我目前測試套件的一個例子。多個提供商Angular 2測試牀

export function main() { 

    describe('Create Applications Component test suite:',() => { 

    let fixtureComponent:any, nativeComponentElement:any; 

    beforeEach(() => { 

     return TestBed.configureTestingModule({ 
     imports: [ 
     ReactiveFormsModule, 
     FormsModule 
     ], 
     declarations: [CreateApplicationComponent] 
     }).overrideComponent(CreateApplicationComponent, { 
     set: { 
      providers: [ 
      { 
       provide: injector, 
       useClass: MockCreateApplicationService 
      } 
      ] 
     } 
     }) 
     .compileComponents().then(() => { 
     fixtureComponent = TestBed.createComponent(CreateApplicationComponent); 
     nativeComponentElement = fixtureComponent.nativeElement; 
     return fixtureComponent; 
     }); 
    }); 

    it('component should work without errors',() => { 
     expect(true).toBe(true); 
    }); 

    }); 

} 

我需要去將是這樣的:

.overrideComponent(CreateApplicationComponent, { 
    set: { 
     providers: [ 
     { 
      provide: injector, 
      useClass: MockCreateApplicationService 
     }, 
     { 
      provide: providerB, 
      useClass: SecondProvider 
     } 
     ] 
    } 
    }) 
+2

'我需要去將是這樣的:'什麼似乎是它的問題? –

+0

當我添加第二個提供程序時,出現錯誤,提示「沒有提供程序的表單構建器」,因此我只是將該表單構建器添加爲overrideComponent方法中的其他提供程序,並且一切正常。謝謝你的慰問! –

回答

0

原來,是添加多個供應商的正確方法。我只是因爲缺少formBuilder提供程序而收到的錯誤而感到困惑,所以我補充說現在一切都按預期工作。

解決方案:

.overrideComponent(CreateApplicationComponent, { 
    set: { 
     providers: [ 
     { provide: CreateApplicationService, useClass: MockCreateApplicationService }, 
     { provide: UserService, useClass: MockUserService }, 
     { provide: FormBuilder, useClass: FormBuilder } 
     ] 
    } 
    })