Angular 在ngOnInit中测试可观测区间
我已经试着让这个测试运行了一天多了,我已经没有选择了 我已经尝试使用了Angular 在ngOnInit中测试可观测区间,angular,typescript,rxjs,Angular,Typescript,Rxjs,我已经试着让这个测试运行了一天多了,我已经没有选择了 我已经尝试使用了fakeAsync的变体,并注入了testScheduler,但是tick()函数似乎没有效果,正如我将要描述的,注入testScheduler效果太好了 我想测试的功能是: ngOnInit(){ ... 间隔(environment.verificationCheckPollingInterval).pipe(takeUntil(this.destroy)).subscribe(\u=> this.store.dispat
fakeAsync
的变体,并注入了testScheduler
,但是tick()
函数似乎没有效果,正如我将要描述的,注入testScheduler
效果太好了
我想测试的功能是:
ngOnInit(){
...
间隔(environment.verificationCheckPollingInterval).pipe(takeUntil(this.destroy)).subscribe(\u=>
this.store.dispatch(fromcount.checkEmailVerificationStatus());
}
我确实意识到,电子邮件验证投票并不是理想的做法,但这是另一天的故事
我的jasmine测试函数如下所示:
description('在验证间隔过后',()=>{
它('应在验证间隔结束后发送检查验证电子邮件操作',fakeAsync(()=>{
勾选(environment.environment.verificationCheckPollingInterval);
fixture.detectChanges();
testScheduler.run({expectObservable,flush,cold})=>{
expectObservable(store.scannedActions$.pipe(
类型(fromAccount.ActionTypes.checkEmailVerificationStatus),
map(()=>true)).toBe('a',{a:true});
});
}));
});
勾选
功能似乎什么也不做
另一种方法是将调度器注入组件,并将订阅更改为
interval(environment.verificationCheckPollingInterval,this.scheduler).pipe(takeUntil(this.destroy)).subscribe(\u=>
this.store.dispatch(fromcount.checkEmailVerificationStatus());
然后在我的测试设置中,包括
{provide:Scheduler,useValue:testScheduler}
但这工作得太好了(除了我将修改生产代码以支持测试这一事实之外,这还远远不够理想)。它工作得太好的原因是,如果我在订阅处理程序上设置断点,它会被重复调用,没有任何延迟,并且在每一步,testScheduler.frame
值都会提前1000
帧(我的轮询间隔是1秒)。这会挂断我的浏览器,使我的笔记本电脑发热,这有点令人担忧
我错过什么了吗?有没有一种方法可以测试interval
,而不用点燃我的笔记本电脑
更新
我有一个非常明智的建议,不幸失败了
鉴于此代码:
description('在验证间隔过后',()=>{
它('应在验证间隔结束后发送检查验证电子邮件操作',(完成)=>{
store.scannedActions$.pipe(
类型(fromAccount.ActionTypes.checkEmailVerificationStatus),
接受(1)。订阅(x=>{
console.log('action dispatched',x);
完成();
});
});
});
调用了console.log
调用,但测试失败并显示消息
错误:超时-在5000ms内未调用异步回调(由设置
jasmine.DEFAULT_TIMEOUT_INTERVAL)
您只需订阅存储
并在完成测试后调用done
欲了解更多信息,请阅读
伟大的建议——不幸的是,它失败了。我将在问题中添加失败的描述。环境的值是什么。验证checkpollinginterval
?您的存储似乎从未启动该操作,或者发送的操作不是Account.ActionTypes.checkEmailVerificationStatus的类型。它肯定在调度操作,因为console.log
调用正在接收。时间是一罐蠕虫。在这种情况下,为什么还要使用interval?你也可以共享checkEmailVerificationStatus
函数的代码吗?@mamosek——因为我使用的是Firebase,而web套接字选项不可用。在这种情况下,投票是一种务实的妥协。
describe('After the verification interval has elapsed', () => {
it('should dispatch a check verification email action after the verification interval has elapsed', (done) => {
store.scannedActions$.pipe(
ofType(fromAccount.ActionTypes.checkEmailVerificationStatus))
.subscribe(_ => {
// run your tests here
done();
});
});
});