TypeError:无法读取未定义的属性(正在读取';_Value';) [英] TypeError: Cannot read properties of undefined (reading '_value')
本文介绍了TypeError:无法读取未定义的属性(正在读取';_Value';)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为以下方法编写测试用例:-
constructor(private dataSharing: DataSharingService) {
const res: any = this.dataSharing.getSystemUser();
this.systemUserData = res.source._value;
this.systemUserData.systemUserId && this.systemUserData.systemUserId === 1 ? this.disableButton = false : this.disableButton = true;
}
我已经在我的spec.ts文件中尝试了以下代码片段来覆盖上面的代码:-
fdescribe('ManagePermissionsComponent', () => {
let component: ManagePermissionsComponent;
let fixture: ComponentFixture<ManagePermissionsComponent>;
let dataSharing = jasmine.createSpyObj('DataSharingService', ['getSystemUser']);
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
declarations: [ManagePermissionsComponent],
providers: [
{provide: DataSharingService, dataSharing: roleServiceStub }, SessionStorageService]
})
.compileComponents();
});
beforeEach(() => {
component.systemUserData = dataSharing.getSystemUser.source._value
//component.systemUserData = {'username': 'akvishwakarma@netlink.com', 'firstName': 'Avani', 'lastName': 'Vishwakarma', 'systemUserId': 1, 'isActive': true, 'password' :"Avani123", 'oldPassword':'Avani123', 'email':'abhargav@gmail.com', 'contact':123, 'imageId':'jpg', 'isAccountLocked':true, 'accountLocked':'dds','accountLockedDate':null, 'loginAttempt':null, 'createdDate':null, 'createdBy':'aparna', 'updatedBy':'jdsjsd', 'updatedDate':null, 'otpGenerated':'dfdsf', 'otpGeneratedDate':'sdsd'};
fixture = TestBed.createComponent(ManagePermissionsComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
});
但我遇到以下错误:-
推荐答案
我已在下面添加了注释,它应该会对您有所帮助。
fdescribe('ManagePermissionsComponent', () => {
let component: ManagePermissionsComponent;
let fixture: ComponentFixture<ManagePermissionsComponent>;
// change this line to just a declaration like so
let dataSharing: jasmine.SpyObj<DataSharingService>;
beforeEach(async () => {
// move the assigning of the spy object here so you have a new
// spy object for every test (beforeEach)
dataSharing = jasmine.createSpyObj<DataSharingService>('DataSharingService', ['getSystemUser']);
await TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
declarations: [ManagePermissionsComponent],
providers: [
// this line was wrong as well, it should be useValue: dataSharing.
// every time the test requires DataSharingService, we provide the mock
{provide: DataSharingService, useValue: dataSharing }, SessionStorageService]
})
.compileComponents();
});
beforeEach(() => {
// need to mock getSystemUser before createComponent because
// we need it for the constructor.
// mock _value however you like
dataSharing.getSystemUser.and.returnValue({ source: { _value: {} }});
fixture = TestBed.createComponent(ManagePermissionsComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
});
这篇关于TypeError:无法读取未定义的属性(正在读取';_Value';)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文