TypeError:无法读取未定义的属性“ employeeName”

时间:2019-05-29 00:21:28

标签: angular jasmine

TypeError:无法读取未定义的属性'employeeName'。 我有一个从服务中获取价值的构造函数订阅。 值不是未定义的,但是在茉莉花单元文本中,它表示未定义。我是茉莉花单元测试的新手,并希望获得支持

在file.ts中,定义了employee的值...

export class WorkspacesComponent implements OnInit {
private employee: Employee
public user: User

    constructor(private workspacesService: WorkspacesService, private userService: UserService, private modalService: NgbModal) {
        this.userService.user$.subscribe(user => { this.user = user });
        this.userService.employee$.subscribe(employee => { this.employee = employee; });
    }

    ngOnInit(): void {
        this.newForm();
        this.getWorkspaces();
        this.user.name = this.employee.employeeName
    }
}

在测试中显示未定义的.spec.ts

const employee: Employee = {

    UNIORG: "1460011",
    companyId: "0001000493",
    companyName: "EVERIS BRASIL CONSULTORIA DE NEGÓCIOS E TECNOLOGIA DA INFORMAÇÃO LTDA",
    departmentId: "08468",
    departmentName: "Tecnologia da Informação",
    employeeName: "LEONARDO COSTA E SILVA DE SOUZA DOS REMÉDIOS",
    endDateSituation: "",
    mail: "",
    managerId: "000699871",
    occupationId: "PPPPPP",
    occupationName: "PRESTADOR DE SERVIO",
    salaryGroupLevel: "0",
    situation: "01",
    startDateSituation: ""
}

const user: User = {
    id: '',
    name: '',
    enrollment: '',
    site: ''
}

const MockUserService = {
    user$: { subscribe: () => { user } },
    employee$: { subscribe: () => { employee } }
};

describe('WorkspacesComponent', () => {

    let component: WorkspacesComponent;
    let fixture: ComponentFixture<WorkspacesComponent>;
    let WorkspacesServiceSpy: WorkspacesService;
    let UserServiceSpy: UserService;
    let modalSpy: NgbModal;


    configureTestSuite(() => {
        TestBed.configureTestingModule({
            schemas: [NO_ERRORS_SCHEMA],
            declarations: [WorkspacesComponent],
            imports: [BrowserModule, FormsModule, MaterialModule, ReactiveFormsModule, BrowserAnimationsModule,],
            providers: [
                { provide: WorkspacesService, useValue: MockWorkspaces },
                { provide: UserService, useValue: MockUserService },
                { provide: NgbModal, useValue: MockNgbModal },
            ]
        })
            .compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(WorkspacesComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        WorkspacesServiceSpy = fixture.debugElement.injector.get(WorkspacesService);
        UserServiceSpy = fixture.debugElement.injector.get(UserService);
        modalSpy = fixture.debugElement.injector.get(NgbModal);
    })

    beforeEach(async(() => {
        fixture.detectChanges();
    }));


    it('should create component', () => {
        expect(component).toBeTruthy();
    });
});

在提出建议后,我进行了更改。但是现在的问题是:

'WorkspacesComponent应该创建组件 TypeError:无法读取未定义的属性“名称”

export class WorkspacesComponent implements OnInit {
private employee: Employee
public user: User

    constructor(private workspacesService: WorkspacesService, private userService: UserService, private modalService: NgbModal) {
        this.userService.user$.subscribe(user => { this.user = user });
        this.userService.employee$.subscribe(employee => { this.employee = employee; });
    }

    ngOnInit(): void {
        this.newForm();
        this.getWorkspaces();
        this.userService.user$.subscribe(user => { this.user = user });
        this.userService.employee$.subscribe(employee => { this.employee = employee; this.user.name = this.employee.employeeName; });
    }
}

1 个答案:

答案 0 :(得分:0)

将您的订阅放入ngOnInit中,比使用构造函数更好。 然后移动它:

this.user.name = this.employee.employeeName;

在您的subscribe()回复中

希望这会有所帮助