我正在尝试使用创建一个Angular Component
,TestBed
但出现以下错误:
错误:无法解析 PriDateInput 的所有参数:(?)。错误属性:Object({ ngSyntaxError: true })
该组件PriDateInput
具有AppContextService
应注入的服务。我想AppContextService
通过替换它来模拟MockAppContextService
.
跟随.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();
}
......
}
如果您认为您需要更多信息,请询问:)