Error:- Can't read property variable of undefined during unit testing
Error:- Can't read property variable of undefined during unit testing
来自服务的变量未定义
下面是我的ts文件
ngOnInit() {
this.serviceElectionForm.form = this.formBuilder.group({});
/****
* commenting below, if condition, doesnt cause any error in spec file
*/
if (this.registerService.submitReg.isAdminFlow) {
this.companyDesignationForm.form = this.formBuilder.group({});
}
this.userAuthenticationForm.form = this.formBuilder.group({});
this.roleDesignationForm.form = this.formBuilder.group({});
}
下面是我的规格文件
fdescribe('RegisterComponent', () => {
let component: RegisterComponent;
let fixture: ComponentFixture<RegisterComponent>;
const MockRegisterService = {
isAdminFlow: true,
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
RegisterComponent,
],
imports: [
AngularMaterialsGlobalModule,
RouterTestingModule,
ReactiveFormsModule,
NoopAnimationsModule,
HttpClientTestingModule
],
providers: [
{ provide: RegisterService, useValue: MockRegisterService },
CookieService
]
}).compileComponents();
}));
describe('Base instantiation and component creation', () => {
beforeEach(() => {
fixture = TestBed.createComponent(RegisterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create register component ', () => {
expect(component).toBeTruthy();
});
});
我得到的错误是
typeError: 无法读取未定义的 属性 'isAdminFlow'
类型错误:无法读取未定义的 属性 'isAdminFlow'
在 RegisterComponent../src/app/pages/registration/register.component.ts.RegisterComponent.ngOnInit
试试这个,您的服务期望在 属性 所在的 isAdminFlow
中有一个名为 submitReg
的 属性
const MockRegisterService = {
submitReg:{
isAdminFlow: true,
}
}
来自服务的变量未定义
下面是我的ts文件
ngOnInit() {
this.serviceElectionForm.form = this.formBuilder.group({});
/****
* commenting below, if condition, doesnt cause any error in spec file
*/
if (this.registerService.submitReg.isAdminFlow) {
this.companyDesignationForm.form = this.formBuilder.group({});
}
this.userAuthenticationForm.form = this.formBuilder.group({});
this.roleDesignationForm.form = this.formBuilder.group({});
}
下面是我的规格文件
fdescribe('RegisterComponent', () => {
let component: RegisterComponent;
let fixture: ComponentFixture<RegisterComponent>;
const MockRegisterService = {
isAdminFlow: true,
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
RegisterComponent,
],
imports: [
AngularMaterialsGlobalModule,
RouterTestingModule,
ReactiveFormsModule,
NoopAnimationsModule,
HttpClientTestingModule
],
providers: [
{ provide: RegisterService, useValue: MockRegisterService },
CookieService
]
}).compileComponents();
}));
describe('Base instantiation and component creation', () => {
beforeEach(() => {
fixture = TestBed.createComponent(RegisterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create register component ', () => {
expect(component).toBeTruthy();
});
});
我得到的错误是
typeError: 无法读取未定义的 属性 'isAdminFlow'
类型错误:无法读取未定义的 属性 'isAdminFlow'
在 RegisterComponent../src/app/pages/registration/register.component.ts.RegisterComponent.ngOnInit
试试这个,您的服务期望在 属性 所在的 isAdminFlow
submitReg
的 属性
const MockRegisterService = {
submitReg:{
isAdminFlow: true,
}
}