Angular Effects - how to get 100 percent test result (coverage in funcs )?

亡梦爱人 提交于 2021-02-11 13:40:51

问题


here is my effects:

import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of, Observable } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import * as courseAttributeActions from '../actions/candidate-registration-management.action';
import { CandidateRegistrationManagementService } from '../../services/candidate-registration-management.service';

@Injectable()
export class CandidateRegistrationEffects {
    constructor(
        private actions$: Actions, 
        private courseAttributeService:
        CandidateRegistrationManagementService) { }
  
    /**
     * getting assessment periods
     */
    getAssessmentPeriods$: Observable<Action> = createEffect(() =>
        this.actions$.pipe(
            ofType(courseAttributeActions.getAssessmentPeriod_CR),
            switchMap(() => this.courseAttributeService.getAssessmentPeriods()
                .pipe(
                    map((value: any) => {
                        return courseAttributeActions.getAssessmentPeriodSuccess_CR({ payload: value });
                    }),
                    catchError(error => of(courseAttributeActions.LoadFailure_CR(error)))
                ))
        ));

}

spec file:

import { cold, hot } from 'jasmine-marbles';
import { Observable, of } from 'rxjs';
import { CandidateRegistrationManagementService } from '../../services/candidate-registration-management.service';
import * as CRMActions from '../actions/candidate-registration-management.action';
import { CandidateRegistrationEffects } from './candidate-registration-management.effect';

describe("Candidate registration management effects", () => {
    let actions: Observable<any> = of([]);
    let CRMService: CandidateRegistrationManagementService;
    let CRMEffects: CandidateRegistrationEffects;

    it('should load and dispatch correctly for getAssessmentPeriods', () => {

        const values = [];

        const http = {
            get: jest.fn(() => of(hot('--b|', { b: { payload: values } })))
        };

        CRMService = new CandidateRegistrationManagementService(http as any);
        CRMEffects = new CandidateRegistrationEffects(actions, CRMService);

        const action = CRMActions.getAssessmentPeriod_CR();
        const completion = CRMActions.getAssessmentPeriodSuccess_CR({ payload: values });

        const expected = of(cold('--b|', { b: completion }));
        expect(CRMEffects.getAssessmentPeriods$).toBeTruthy();

    })

})

works fine. but getting only 40% of Funcs from the above. looking for 100% of funcs covered. any help me?


回答1:


I don't use marbles for testing effects (feels like magic and steep learning curve).

class MockCourseAttributeService {
  getAssessmentPeriods() {
    return of({});
  }
}

class MockCourseAttributeServiceError {
  getAssessmentPeriods() {
    return throwError('Service Failed');
  }
}

describe('CandidateRegistrationEffects', () => {
  let actions$: ReplaySubject<any>;
  let effects: CandidateRegistrationEffects; 
  describe('Effects are successful', () => {
    beforeEach(() => {
      TestBed.configureTestingModule({
        providers: [{ provide: CandidateRegistrationManagementService, useClass: MockCourseAttributeService }, CandidateRegistrationEffects, provideMockActions(() => actions$)],
      });
      effects = TestBed.get(CandidateRegistrationEffects);
      actions$ = new ReplaySubject(1);
    });

    it('should load the assesment periods', () => {
      actions$.next(courseAttributeActions.getAssessmentPeriod_CR());
      effects.getAssessmentPeriods$.subscribe((value) => {
        expect(value).toEqual(courseAttributeActions.getAssessmentPeriodSuccess_CR({ payload: value }));
      });
    });
  });

  describe('Effects are Failure', () => {
    beforeEach(() => {
      TestBed.configureTestingModule({
        providers: [{ provide: CandidateRegistrationManagementService, useClass: MockCourseAttributeServiceError }, CandidateRegistrationEffects, provideMockActions(() => actions$)],
      });
      effects = TestBed.get(CandidateRegistrationEffects);
      actions$ = new ReplaySubject(1);
    });

    it('Gender load Fails', () => {
      actions$.next(courseAttributeActions.getAssessmentPeriod_CR());
      effects.loadGender$.subscribe((error) => {
        expect(error).toEqual(courseAttributeActions.LoadFailure_CR(error));
      });
    });
  });
});

I think this should work.



来源:https://stackoverflow.com/questions/62771062/angular-effects-how-to-get-100-percent-test-result-coverage-in-funcs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!