简体   繁体   English

observable 的特殊结果:用茉莉花大理石进行测试

[英]Peculiar result from observable: testing with Jasmine marbles

I have a little function in Angular 7 that I am testing with Jest.我在 Angular 7 中有一个小功能,我正在用 Jest 进行测试。 The function looks like this:该函数如下所示:

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

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

}

As you can see, for the moment all it is doing is creating an observable from its input, but it is under development and will be extended.如您所见,目前它所做的只是根据其输入创建一个可观察对象,但它正在开发中并将进行扩展。

I am testing it with Jest like this:我正在用 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);

});

However, the test is failing because of some timing issue.但是,由于某些时间问题,测试失败。 The expected observable result looks like this: 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
    }
  }
]

and the observable created from the function call hotspotService['checkFreeProduct'](allowance) looks like this:从函数调用hotspotService['checkFreeProduct'](allowance)创建的 observable 如下所示:

[
  {
    "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
    }
  }
]

Now I'm not exactly sure why there are two emissions from these observables, but I'll go with it.现在我不确定为什么这些可观测值会产生两次排放,但我会同意。 What I don't understand is why the observable from the function call emits two events, both on frame 0. I have tried both hot() and cold() , and have experimented with various marbles in these calls but no joy.我不明白的是为什么函数调用中的 observable 会在第 0 帧发出两个事件。我尝试了hot()cold() ,并在这些调用中尝试了各种弹珠,但没有任何乐趣。 Can someone please explain?有人可以解释一下吗?

The question you asked about two events emitted is because of the marble hot('a|') - 1st one is emitting the value that you wish to assert 'a' and second is emitted to indicate the completion of the hot observable '|'您询问的关于发出的两个事件的问题是因为弹珠hot('a|') - 第一个是发出您希望断言 'a' 的值,第二个是发出来指示热可观察的'|' . . This can be inferred from the Notification -> Kind property in those events that you have added in your question.这可以从您在问题中添加的那些事件中的 Notification -> Kind 属性推断出来。 Ex: "kind": "N" -> Value emitted.例如:“种类”:“N”-> 发出的值。 kind": "C" -> completion of observable. kind": "C" -> observable 的完成。

Answer : To fix your unit test, just change the marble to '(a|)' , so that both of them are emitted in the same time frame.:要修复您的单元测试,只需将弹珠更改为'(a|)' ,以便它们在同一时间范围内发出。 I have tested this and it works.我已经测试过这个并且它有效。

Reason for different time frames: The hot and cold create observable streams that emit values on specific time intervals.不同时间范围的原因:热和冷创建可观察的流,在特定的时间间隔发出值。 Marbles denote actions that happen over time on observables. Marbles 表示随着时间的推移在 observable 上发生的动作。

  • - - denotes a unit of time frame. - - 表示时间单位。
  • [a-z0-9] - denotes values emitted from stream. [a-z0-9] - 表示从流中发出的值。
  • | - denotes completion of observable stream. - 表示可观察流的完成。
  • # - denotes error in observable stream. # - 表示可观察流中的错误。
  • () - denotes grouping of values emitted on the same time frame. () - 表示在同一时间范围内发出的值的分组。

For your solution , hot('(a|')) - means emit both the value a and complete the observable stream within the same time frame.对于您的解决方案hot('(a|')) - 表示在同一时间范围内发出值a并完成可观察流。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM