如何解决使用单元测试在RxJS管道内执行逻辑
在进行单元测试时,尝试在pipe()方法内部初始化变量x,但不会被调用。如何在pipe()中运行方法调用/初始化逻辑?仅仅在单元测试中调用component.ngOnInit()
并没有帮助。
ngOnInit() {
this.foo$ = this.myService.bar$.pipe(
....
map(bar => {
this.x = bar.x; // how do I call this within unit test
return bar;
})
);
}
it('should init x',() => {
component.ngOnInit();
expect(component.x).toEqual(bar.x); //fails
});
解决方法
您可能想订阅然后打勾
import { fakeAsync,tick } from '@angular/core/testing';
it('should init x',fakeAsync(() => {
component.ngOnInit();
component.foo$.subscribe();
tick();
expect(component.x).toEqual(bar.x);
}));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。