angular - 模拟订阅变量
问题描述
我有一个名为 stateSubscition$ 的变量,我用它来保存和取消订阅订阅,它运行良好。但我需要知道如何模拟 stateSubscription$ 为 ondes 编写单元测试用例
stateSubscription$: Subscription;
...
this.stateSubscription$ = this.updatedScheduleActivities$.subscribe((x) => {
this.loadGrid();
});
...
ngOnDestroy() {
this.stateSubscription$.unsubscribe();
}
并在规范文件中
fdescribe('#onDestroy', () => {
let component;
beforeEach(() => {
component = new VariableComponent(mockStore, mockForm);
spyOnProperty(component,'stateSubscription$','get').and.returnValue(Observable);
component.ngOnDestroy();
});
it('should have called unsubscribe method', () => {
expect(component.stateSubscription$.unsubscribe).toHaveBeenCalled();
});
});
我收到这样的错误,如何模拟这个属性?
Error: stateSubscription$ property does not exist
解决方案
你总是可以stateSubcription$
用一个空来初始化Subscription
stateSubscription$ = new Subscription();
undefined
这样您在访问该物业时就不必担心。
推荐阅读
- node.js - 如何更新 mongodb atlas 文档中的值?
- javascript - If 语句 - Javascript/HTML 数学测验
- python - Python Discord Bot - 在调用函数时向特定频道发布消息
- macos - 在 macOS 中无法启动 Leap Motion Home 应用程序
- zsh - 检查是否设置了“以x的值命名的参数”
- javascript - 如何在js中正确使用closest()
- c++ - GLFW:glfwSetGammaRamp 函数不起作用
- python - 将元素标签添加到现有的 html
- swiftui - SwiftUI 两行导航栏
- javascript - 减少不保存在变量值中