【问题标题】:Angular 2 Unit test Error: Cannot resolve all parameters DecoratorFactory:(?)Angular 2单元测试错误:无法解析所有参数DecoratorFactory:(?)
【发布时间】:2017-11-13 16:21:36
【问题描述】:

我想测试一个具有一些依赖项的简单组件。因此,除其他外,我必须提供一些提供者 describe('AccountLookupComponent', () => { 让组件:AccountLookupComponent; 让夹具:ComponentFixtureenter code here;

    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 {


}

【问题讨论】:

    标签: angular typescript karma-runner


    【解决方案1】:

    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
    

    【讨论】:

    • 当我删除 NgModule 时,它​​给了我这个错误 No provider for NgbModal!
    • 导入 NgbModule.forRoot() 或 NgbModule.forRoot() 代替
    【解决方案2】:

    使用提供者测试组件有两种方法

    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);
    

    【讨论】:

    • 您正在尝试在 Providers 数组中导入装饰器。为什么要将 NgModule 添加到提供程序?它应该只包含服务。
    • 我现在是装饰器,但它给了我这个错误 :::inline template:0:0 原因是:No provider for NgbModal!
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2017-11-20
    • 2017-02-11
    • 1970-01-01
    • 2021-04-17
    • 2018-01-07
    • 2016-07-29
    • 2017-06-17
    相关资源
    最近更新 更多