2017-06-12 50 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的提供者! –

相关问题