TypeError: Cannot read properties of undefined (reading '_value')

TypeError: Cannot read properties of undefined (reading '_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();
    });