4

我正在使用 Jest 测试 Angular 7 中的一个小功能。该函数如下所示:

private checkFreeProduct(allowance: SubscriberConnectivityAllowanceInterface): Observable<SubscriberConnectivityAllowanceInterface> {

    // TODO: This is currently just a temp function to be extended when required
    return of(allowance);

}

如您所见,目前它所做的只是从其输入创建一个可观察对象,但它正在开发中并将被扩展。

我正在像这样用 Jest 测试它:

it('should return an observable of the allowance', () => {

    const allowance: SubscriberConnectivityAllowanceInterface = {
        hotspotAuthenticated: HotspotAuthenticationEnum.TRUE,
        remainingOctets: 100,
        remainingSeconds: 200,
        activeProductCost: ConnectivityProductCostEnum.PAID,
        activeProductDuration: ConnectivityProductDurationEnum.FLIGHT,
        activeProductType: ConnectivityProductTypeEnum.PREMIUM,
        connectivityProducts: []
    };

    const expected = hot('a|', {
        a: allowance
    });

    expect(hotspotService['checkFreeProduct'](allowance)).toBeObservable(expected);

});

但是,由于一些时间问题,测试失败了。可expected观察的结果如下所示:

[
  {
    "frame": 0,
    "notification": {
      "error": undefined,
      "hasValue": true,
      "kind": "N",
      "value": {
        "activeProductCost": "paid",
        "activeProductDuration": "flight",
        "activeProductType": "premium",
        "connectivityProducts": [],
        "hotspotAuthenticated": 1,
        "remainingOctets": 100,
        "remainingSeconds": 200
      }
    }
  },
  {
    "frame": 10,
    "notification": {
      "error": undefined,
      "hasValue": false,
      "kind": "C",
      "value": undefined
    }
  }
]

从函数调用创建的 observablehotspotService['checkFreeProduct'](allowance)如下所示:

[
  {
    "frame": 0,
    "notification": {
      "error": undefined,
      "hasValue": true,
      "kind": "N",
      "value": {
        "activeProductCost": "paid",
        "activeProductDuration": "flight",
        "activeProductType": "premium",
        "connectivityProducts": [],
        "hotspotAuthenticated": 1,
        "remainingOctets": 100,
        "remainingSeconds": 200
      }
    }
  },
  {
    "frame": 0, // <------- this is the only difference
    "notification": {
      "error": undefined,
      "hasValue": false,
      "kind": "C",
      "value": undefined
    }
  }
]

现在我不完全确定为什么这些可观测物会产生两种排放,但我会同意的。我不明白为什么函数调用中的 observable 会在第 0 帧上发出两个事件。我都尝试过hot()and cold(),并在这些调用中尝试了各种弹珠,但没有任何乐趣。有人可以解释一下吗?

4

1 回答 1

5

你问的关于发射的两个事件的问题是因为弹珠hot('a|')- 第一个是发射你希望断言 'a' 的值,第二个是发射以指示 hot observable 的完成'|'。这可以从您在问题中添加的那些事件中的 Notification -> Kind 属性中推断出来。例如:“种类”:“N”-> 发出的值。kind": "C" -> observable 的完成。

:要修复您的单元测试,只需将弹珠更改为'(a|)',以便它们在同一时间范围内发出。我已经对此进行了测试,并且可以正常工作。

不同时间框架的原因: 热和冷创建可观察的流,在特定时间间隔发出值。Marbles 表示随着时间的推移在 observables 上发生的动作。

  • -- 表示时间范围的单位。
  • [a-z0-9]- 表示从流中发出的值。
  • |- 表示可观察流的完成。
  • #- 表示可观察流中的错误。
  • ()- 表示在同一时间范围内发出的值的分组。

对于您的解决方案, - 表示在同一时间范围内hot('(a|'))发出值并完成可观察流。a

参考资料: https ://github.com/jisaacks/RxJS/blob/master/doc/writing-marble-tests.md https://medium.com/@bencabanes/marble-testing-observable-introduction-1f5ad39231c

于 2020-03-12T08:20:11.730 回答