我正在实施效果测试。
测试给出了错误:
[{"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 中进行一些配置以识别测试。感谢所有花时间帮助我的人。