2017-06-12 71 views
0

我想測試一個具有一些依賴關係的簡單組件。所以除其他我必須提供一些提供商 描述( 'AccountLookupComponent',()=> { 令組分:AccountLookupComponent; 設夾具:ComponentFixture enter code here;Angular 2單元測試錯誤:無法解析所有參數DecoratorFactory :(?)

beforeEach(async(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ 
       TestComponentWrapper, 
       AccountLookupComponent 
      ], 
      schemas: [CUSTOM_ELEMENTS_SCHEMA], 
      imports: [HttpModule], 
      providers: [AccountLookupService, AuthHttp, AuthenticationService, AdalService, AdfsSecretService, CookieService, NgModule, 
       { provide: 'IAccountLookupClient', useClass: AccountLookupClient }, 
       { provide: 'IApiClient', useClass: ApiClient }, 
       { provide: 'ITimeoutService', useClass: TimeoutService }, 

      ] 

     }) 
      .compileComponents(); 

     fixture = TestBed.createComponent(TestComponentWrapper); 
     component = fixture.debugElement.children[0].componentInstance; 
     fixture.detectChanges(); 
    })); 

    it('should create',() => { 
     expect(component).toBeTruthy(); 
    }); 
}); 

@Component({ 
    selector: 'test-component-wrapper', 
    template: `<account-lookup filterCurrentAccount="true" 
           [useAccountIdSearch]="true" 
           [useCompactResults]="true" 
           (accountSelected)="null" 
           placeholder="Find account"> 
       </account-lookup>`, 

}) 
class TestComponentWrapper { 


} 

回答

0

providers陣列中刪除NgModule。這是裝飾者。你不應該用它作爲標記。

export function makeDecorator(
    name: string, props?: (...args: any[]) => any, parentClass?: any, 
    chainFn?: (fn: Function) => void): (...args: any[]) => (cls: any) => any { 
    const metaCtor = makeMetadataCtor(props); 

    function DecoratorFactory(objOrType: any): (cls: any) => any { // => here is your provider 
+0

當我刪除NgModule它給了我這個錯誤沒有提供NgbModal! –

+0

導入NgbModule.forRoot()或NgbModalModule.forRoot()而不是 – yurzui

0

有,測試是否可以提供部件雙向

1.注入真正的服務。

2.使用測試雙打(存根,假貨,間諜,或嘲笑)。

第一方法的組件沒有與真正的服務被注入,但你可以做這樣的事情例如:

TestBed.configureTestingModule({ 
    declarations: [ ExampleComponent ], 
    providers: [ ExampleService ]] 
}); 

另一種方法是使用存根例如:

TestBed.configureTestingModule({ 
    declarations: [ ExampleComponent ], 
    // Provide a test-double instead 
    providers: [ {provide: ExampleService, useValue: exampleServiceStub }] 
}); 

// UserService from the root injector 
exampleService = TestBed.get(ExampleService); 
+0

請參閱我的代碼 –

+0

您正在嘗試導入Providers數組中的裝飾器。爲什麼要將NgModule添加到提供者?它應該只包含服務。 – esso

+0

我現在是它的裝飾器,但它給了我這個錯誤:::內聯模板:0:0引起:沒有NgbModal的提供者! –

相關問題