mardi 22 novembre 2016

How to write the test case for testing function which calls service in karma (Angular2)

I am trying to write the test case for the function which makes the service call for the

here is my function on component

public showActivityList = () => {
        this.display = !this.display;
        if (this.incompleActivities.length > 0) { return; }
        this._activityService.getIncompleteActivity(this.id, SortTypeMap[this.tab], this.role, this.status)
            .subscribe((res) => {
                res.forEach(obj => {
                    this.incompleActivities.push(this.UpdateStatus(obj));
                })
            });
    }

here is my service function

public getIncompleteActivity(activityId: string, tab: string, role: string, status: string): Observable<Array<IncompleteActivitiesModel>> {
        let IncompleteActivitiesSubject = new ReplaySubject<Array<IncompleteActivitiesModel>>(1);
        let paramObj = {
            'activityId': activityId,
            'currentTab': tab,
            'role': role,
            'status': status
        }
        this.schAPIs.getIncompleActivities(paramObj)
            .subscribe((res) => {
                IncompleteActivitiesSubject.next(res);
            });
        return IncompleteActivitiesSubject
    }

here is my test in karma

it('should call getIncompleteActivity when click on count',
        inject([XHRBackend, ExeDashboardActivityService], (mockBackend: MockBackend, service: ExeDashboardActivityService) => {

            mockBackend.connections.subscribe(
                (connection: MockConnection) => {
                    connection.mockRespond(new Response(
                        new ResponseOptions({
                            body: [
                                {
                                    dueDateTime: 1470421799999,
                                    id: 176515,
                                    status: "CREATED",
                                    title: "4th August Sch 04"
                                }]
                        }
                        )));
                });
            comp.id = 176537, comp.tab = "GROUP", comp.role = "assignee", comp.status = "incomplete";
            fixture.detectChanges();
            service.getIncompleteActivity(comp.id, comp.tab, comp.role, comp.status).subscribe((incompleActivities: IncompleteActivitiesModel[]) => {
                expect(incompleActivities.length).toBe(1);
                expect(incompleActivities[0].id).toBe(176451);
            });
        }));

but when I run my test it throws an error

TypeError: Cannot read property 'subscribe' of undefined

Please correct me if I making any mistake.

Aucun commentaire:

Enregistrer un commentaire