【问题标题】:How can i test this function in Angular 2我如何在 Angular 2 中测试这个功能
【发布时间】:2022-01-17 10:30:53
【问题描述】:
moveColumns(shifter: number, index: number) {
    const columnFields = this.form.get('sizes') as FormArray;
 
    let newIndex = index + shifter;
    if (newIndex === -1) {
      newIndex = columnFields.length - 1;
    } else if (newIndex === columnFields.length) {
      newIndex = 0;
    }
 
    const currentGroup = columnFields.at(index);
    columnFields.removeAt(index);
    columnFields.insert(newIndex, currentGroup);
  }

我的难点是它们都在函数变量内部,而不是组件全局变量。

【问题讨论】:

  • 你需要存根表单对象,然后通过这个函数比较对象中发生的变化

标签: angular unit-testing jasmine karma-jasmine


【解决方案1】:

您需要存根表单对象,然后验证函数中发生的更改

// in your test beforeEach
mockComponent.form = new FormArray({sizes: new FormControl());

// And in your function, you can update this form object and validate it

it('should test my function', () => {
const modifiedForm = {} // Add the final result after the function executes

expect(mockComponent.form).toEquals(modifiedForm);
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多