0

我正在实施效果测试。

测试给出了错误:

[{"frame": 10, "notification": {"error": [TypeError: Cannot read
property 'getDowntimeOfProductShiftGraphic' of undefined], "hasValue":
false, "kind": "E", "value": undefined} }]

getDowntimeOfProductShiftGraphic在我的服务中是一种方法。

我遵循了这个项目的指示以及 Jesse Sanders 关于在 ng-conf 中进行Jest 测试的谈话。

下面是我的部分测试代码。

export class TestActions extends Actions {
    constructor() {
        super(empty());
    }

    set stream(source: Observable<any>) {
        this.source = source;
    }
}

export function getActions() {
    return new TestActions();
}

describe('Auth Effects', () => {

    let actions: TestActions;
    let effects: StatisticsEffects;
    let serviceDownTime: IDownTimeRecordService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                StatisticsEffects,
                {
                    provide: Actions,
                    useFactory: getActions
                },
                {
                    provide: IDownTimeRecordService
                },
                {
                    provide: DowntimeRecordService,
                    useValue: { getDowntimeOfProductShiftGraphic: jest.fn() }
                },
                {
                    provide: ApiHttpService
                },
                {
                    provide: IMachineOperationService
                },
                {
                    provide: GlobalEnvironmentService
                },
            ]
        });

        actions = TestBed.get(Actions);
        effects = TestBed.get(StatisticsEffects);
        serviceDownTime = TestBed.get(DowntimeRecordService);
    });

    test('should create effect and services', () => {
      expect(effects).toBeTruthy();
      expect(serviceDownTime).toBeTruthy();
    });

    describe('some description', () => {
        it('should return something', () => {

            const request: DownTimeStatisticsChartRequestModel = {
                productId: 1,
                startDate: new Date()
            };

            const chartData: IShiftGraphicDto[] = [{
                Uptime: 100,
                Downtime: 200,
                Name: 'blabla'
            }];

            const action = new StatisticsActions.GetDownTimeStatisticsChart(request);

            const outcome = new StatisticsActions.GetDownTimeStatisticsChartSuccess(chartData);

            actions.stream = hot('-a', { a: action });
            const response = cold('-a|', { a: chartData });
            const expected = cold('-b', { b: outcome });

            expect(effects.getDownTimeStatisticsChart$).toBeObservable(expected);
        });
    });

更新

我的问题的解决方案是在 webpack 中进行一些配置以识别测试。感谢所有花时间帮助我的人。

4

1 回答 1

0

尝试如下更改预期。希望能帮助到你:

const expected = cold('--b', { b: outcome });

在进行断言时,将其作为

effects.getDownTimeStatisticsChart$.subscribe((actualAction) =>{ expect(actualAction).toBeObservable(expected) });

我希望这会有所帮助

于 2018-06-27T23:21:58.110 回答