【问题标题】:Testbed: Error: Can't resolve all parameters for PriDateInput: (?)测试台:错误:无法解析 PriDateInput 的所有参数:(?)
【发布时间】:2019-03-16 20:05:49
【问题描述】:

我正在尝试使用TestBed 创建Angular Component,但出现以下错误:

错误:无法解析 PriDateInput 的所有参数:(?)。 错误属性:Object({ ngSyntaxError: true })

组件PriDateInput 有一个服务AppContextService 应该被注入。我想通过用MockAppContextService 替换AppContextService 来模拟它。

关注.spec 文件:

class MockWindowService extends WindowService {
    get href() : string {
        return "https://elevationfw-iridium-build.azurewebsites.net/Default/Azure-DEV/#/";
    }
} 

class MockAppContextService extends AppContextService {
    constructor(){
        super(new StorageService(), new MockWindowService());
    }
    getContext() {
        let emptyContext: any = this.emptyContext;
        emptyContext.user.culture = "en-US";
        return this.emptyContext;
    }
} 

describe('AppContextService Test cases', () => {
    let mockApp = new MockAppContextService();
    let priDateInput: PriDateInput;
    debugger;
    beforeEach(()=> {
        TestBed.configureTestingModule({
            declarations: [PriDateInput],
            providers: [
            {
                provide: AppContextService,
                useClass: mockApp
            }
            ],
        });
        priDateInput = TestBed.get(PriDateInput);
    });

    it('should be possible to instantiate it', () => {
        expect(priDateInput).toBeDefined();
        expect(priDateInput).not.toBeNull();
    });

    it('should be possible to instantiate it', () => {

        expect(priDateInput).toBeDefined();
        expect(priDateInput).not.toBeNull();
    });
});

以下是我的组件的摘录:

import { Component, OnInit, OnDestroy, ViewChild, ElementRef, Input, Output, EventEmitter } from "@angular/core";
import { AppContextService } from "./../../../../../services/appcontext.service";
import * as moment from 'moment';
@Component({
    selector: "pri-date-input",
    templateUrl: './input.date.component.html'
})

export class PriDateInput implements OnInit, OnDestroy {
    .............................
    @ViewChild("input") input: ElementRef;

    @Input("domainType") domainType: String;
    @Input("required") required: Boolean;
    @Input("isLoading") isLoading: boolean;
    @Input("isDisabled") isDisabled: boolean;
    @Input("onWriteValue") onWriteValue: EventEmitter<string | null>;
    @Output("onDateChange") onDateChange: EventEmitter<string> = new EventEmitter<string>();

    constructor(
        private _appContextService: AppContextService
    ) {
        moment.locale(this._appContextService.user.culture);
    }

    ngOnInit(): void {
        .......
    }

    ngOnDestroy(): void {
        this.unsubscriveInputEvents();
    }
    ......
}

如果您认为需要更多信息,请询问:)

【问题讨论】:

    标签: angular typescript jasmine karma-runner testbed


    【解决方案1】:

    你创建一个类实例

    let mockApp = new MockAppContextService();
    

    但将其用作类模拟

    {
      provide: AppContextService,
      useClass: mockApp
    }
    

    试试

    {
      provide: AppContextService,
      useValue: mockApp
    }
    

    另外,你错过了一些代码......

    TestBed.configureTestingModule(/* config */)
      .compileComponents();
    
    fixture = TesBed.createComponent(PriDateInput);
    component = fixture.componentInstance;
    fixture.detectChanges();
    

    【讨论】:

    • 错误依旧,这并不能解决我的问题。但你有正确的东西,我不属于。谢谢你:)
    • 我在答案中添加了一个编辑,请随时查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2017-11-13
    • 2016-11-18
    • 1970-01-01
    相关资源
    最近更新 更多