gpt4 book ai didi

angular - 测试平台:错误:无法解析 PriDateInput 的所有参数:(?)

转载 作者:搜寻专家 更新时间:2023-10-30 21:27:08 25 4
gpt4 key购买 nike

我正在尝试使用 TestBed 创建一个 Angular Component 但我收到以下错误:

Error: Can't resolve all parameters for PriDateInput: (?). error properties: Object({ ngSyntaxError: true })

组件 PriDateInput 有一个服务 AppContextService 应该被注入(inject)。我想通过用 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();
}
......
}

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

最佳答案

你创建一个类实例

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

关于angular - 测试平台:错误:无法解析 PriDateInput 的所有参数:(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52765165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com