来自服务的变量是未定义的
下面是我的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:无法读取未定义TypeError的属性“”isAdminFlow“”:无法在RegisterComponent../src/app/pages/registration/register.component.ts.RegisterComponent.ngOnInit
读取未定义的属性“”isAdminFlow“”
发布于 2019-06-18 16:49:35
尝试这样做,您的服务需要在该属性驻留isAdminFlow
的内部拥有一个名为submitReg
的属性
const MockRegisterService = {
submitReg:{
isAdminFlow: true,
}
}
https://stackoverflow.com/questions/56653684
复制