如何对此Ngrx实施进行测试?

时间:2019-04-23 05:04:20

标签: angular typescript ngrx

这是我第一次在Ngrx工作。我找到了一些教程,但我仍然不知道如何进行测试以挖掘Ngrx的实现。我想测试以下代码中的一项操作:

import { Action } from '@ngrx/store';
import { SystemSettings} from './system';

export enum SystemSettingsActionsTypes {
    UpdateSystemSettings = '[SystemSetting] Update Setting',
    UpdateSystemSettingsSuccess = '[SystemSetting] Update Setting Success',
    UpdateSystemSettingsFail = '[SystemSetting] Update Setting Fail',
    LoadSystemSettings = '[SystemSetting] Load Setting',
    LoadSystemSettingsSuccess = '[SystemSetting] Load Setting Success',
    LoadSystemSettingsFail = '[SystemSetting] Load Setting Fail'
}

export class UpdateSystemSettings implements Action {
    public readonly type = SystemSettingsActionsTypes.UpdateSystemSettings;

    constructor(public payload: SystemSettings) { }
}

export class UpdateSystemSettingsSuccess implements Action {
    public readonly type = SystemSettingsActionsTypes.UpdateSystemSettingsSuccess;

    constructor(public payload: SystemSettings) { }
}

export class UpdateSystemSettingsFail implements Action {
    public readonly type = SystemSettingsActionsTypes.UpdateSystemSettingsFail;

    constructor(public payload: string) { }
}

export class LoadSystemSettings implements Action {
    public readonly type = SystemSettingsActionsTypes.LoadSystemSettings;

    constructor() { }
}

export class LoadSystemSettingsSuccess implements Action {
    public readonly type = SystemSettingsActionsTypes.LoadSystemSettingsSuccess;

    constructor(public payload: SystemSettings) { }
}

export class LoadSystemSettingsFail implements Action {
    public readonly type = SystemSettingsActionsTypes.LoadSystemSettingsFail;

    constructor(public payload: string) { }
}

export type SystemSettingsActions =
    | UpdateSystemSettings
    | UpdateSystemSettingsSuccess
    | UpdateSystemSettingsFail
    | LoadSystemSettings
    | LoadSystemSettingsSuccess
    | LoadSystemSettingsFail;

目前,我拥有该代码示例,但是在测试中表现很差,而且仍然不知道如何使其变得更好(我的代码上的良好工作示例将非常有帮助)。

describe('LoadSystemSettingsSuccessfull', () => {
  it('should load system settings successfully', () => {

    const payload: SystemSettings[] = [
      {
        measure: 1,
        clock: 1,
        dateFormat: 1,
        lowQuality: 1,
        highQuality: 1
      }
    ];
    const action = new LoadSystemSettingsSuccess(payload);

    expect({ ...action}).toEqual({
      type: LoadSystemSettingsSuccess,
      payload,
    });
  });
});

1 个答案:

答案 0 :(得分:0)

您的动作测试看起来不错!问问自己Why am I testing this?通常会有所帮助,在这种情况下,您正在测试操作的实现是否会创建您期望的对象。

尽管这些测试方案通常被用作测试NGRX的简介,但它们可能也是最没用的,而且-在我看来-常常不能证明这样做的合理性。

相反,请专注于测试NGRX的以下元素:

  • 减速器
  • 选择器
  • 效果

这些是负责您应用功能的主要构建模块,应进行全面测试。