Tan*_*eel 4 unit-testing karma-jasmine angular
我正在用 Jasmine 和 Karma 学习单元测试。我的测试用例正在通过,但我不明白一件事。这是我的打字稿:
// array list of objects where each month will have one object i.e. 12 objects (Jan to Dec)
monthsData: Array<{
monthName: string;
monthYear: number;
isInRange: boolean;
isLowerEdge: boolean;
isUpperEdge: boolean;
}>;
rangeState: { edge1Exists: boolean; edge2Exists: boolean; edgeIndexes: Array<number> };
initRangeState() {} <---- method which should be called; Not important for this question
Run Code Online (Sandbox Code Playgroud)
这是我在规范文件中的测试用例:
it('should re-initialize range state when reflection is triggered', () => {
fixture.detectChanges(); <--- why this is required ?
const rangeState = { edge1Exists: true, edge2Exists: true, edgeIndexes: [] };
const monthsData = {
monthName: 'Jan',
monthYear: 2020,
isInRange: true,
isLowerEdge: true,
isUpperEdge: false
};
fixture.componentInstance.rangeState = rangeState;
fixture.componentInstance.monthsData[0] = monthsData;
...
expect(fixture.componentInstance.initRangeState).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
我不明白,当我通过虚拟rangeState和monthsData已经。那么为什么我需要首先运行fixture.detectChanges()。我的测试用例没有调用就失败了。请告诉我背后的原因。我确定有些事情我不知道。这fixture.detectChanges()是删除时的屏幕截图:
从https://angular.io/guide/testing:
您必须通过调用 fixture.detectChanges() 来告诉 TestBed 执行数据绑定。
[...]
延迟更改检测是有意且有用的。它让测试人员有机会在 Angular 启动数据绑定和调用生命周期钩子之前检查和更改组件的状态。
| 归档时间: |
|
| 查看次数: |
7110 次 |
| 最近记录: |