且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

TypeError:无法读取未定义的属性(正在读取';_Value';)

更新时间:2022-10-28 12:07:58

我已在下面添加了注释,它应该会对您有所帮助。

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();
    });