我正在使用 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()
,并在这些调用中尝试了各种弹珠,但没有任何乐趣。有人可以解释一下吗?