Невозможно прочитать свойство неопределенной кармы getDashBoardData - PullRequest
0 голосов
/ 11 января 2020

В настоящее время работаю над покрытием кармы. Я получаю ошибку ниже при выполнении ng test

TypeError: Cannot read property 'getDashBoardData' of undefined

Вот мой код компонента

ngOnInit(): void {
    this.dataService.getDashBoardData().subscribe((response) => {
        this.dataSource = new MatTableDataSource(response['studies']);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
    })
}

Вот мое Spe c

describe('DashboardComponent', () => {
    let component: DashboardComponent;
    let fixture: ComponentFixture < DashboardComponent > ;
    let service: DataService;
    let httpMock: HttpTestingController
    beforeEach(async (() => {
        TestBed.configureTestingModule({
                imports: [HttpClientTestingModule, BrowserAnimationsModule, RouterTestingModule, MatMenuModule, MatFormFieldModule, MatInputModule, MatPaginatorModule, MatTableModule, MatSortModule, MatIconModule, MatCardModule],
                declarations: [DashboardComponent],
                providers: [{
                    provide: DataService
                }], //, useValue: MockDataService } ],
                schemas: [CUSTOM_ELEMENTS_SCHEMA],
            })
            .compileComponents();

        service = TestBed.get(DataService);
        httpMock = TestBed.get(HttpTestingController);
    }));

    afterEach(() => {
        httpMock.verify();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(DashboardComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
    it('should retrive via get', () => {
        const dashboarddata: Dashboardinfo[] = [{
                "empid": "212121",
                "Numbr": "0101",
                "Fname": "David",
                "Lname": "Sams",
                "Salary": "60,000",
                "State": "TN",
                "status": "Approved",
                "Date": "08-20-2019"
            },
            {
                "empid": "818181",
                "Numbr": "3063",
                "Fname": "Mark",
                "Lname": "William",
                "Salary": "40,000",
                "State": "DL",
                "status": "Pending",
                "Date": "01-18-2019"
            }
        ];

        service.getDashBoardData().subscribe(dashdata => {
            expect(dashdata.length).toBe(2);
            expect(dashdata).toEqual(dashboarddata);
        });
        const request = httpMock.expectOne(`${service.url}/basicInformation`);
        expect(request.request.method).toBe('GET');
        request.flush(dashboarddata);
    });
});
...