[英]Testing effects with Jest with Angular 5
我正在實施效果測試。
測試給出錯誤:
[{“ frame”:10,“ notification”:{“ error”:[TypeError:無法讀取
屬性“ getDowntimeOfProductShiftGraphic”(未定義),“ hasValue”:
false,“ kind”:“ E”,“ value”:未定義}}]
但是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中進行一些配置以識別測試。 謝謝所有花了一些時間幫助我的人。
嘗試如下更改預期值。 希望能幫助到你:
const expected = cold('--b', { b: outcome });
在做斷言的同時做
effects.getDownTimeStatisticsChart$.subscribe((actualAction) =>{ expect(actualAction).toBeObservable(expected) });
希望對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.