Ale*_*ski 10 protractor ngrx zone.js ngrx-effects angular
我刚刚开始为我的应用程序编写e2e测试,并且遇到了Protractor和ngrx/effects的超时问题.
我每隔几分钟调度一次动作有以下效果:
@Effect() setSessionTimer$ = this.actions$
.ofType(Auth.ActionTypes.SET_SECONDS_LEFT)
.map(toPayload)
.switchMap(secondsLeft => Observable.concat(
Observable.timer((secondsLeft - 60) * 1000).map(_ => new Auth.SessionExpiringAction(60)),
Observable.timer(60 * 1000).map(_ => new Auth.SessionExpiredAction())
));
Run Code Online (Sandbox Code Playgroud)
尝试运行Protractor测试会导致测试超时并出现以下错误,因为Angular不稳定.
失败:超时等待异步Angular任务在11秒后完成.这可能是因为当前页面不是Angular应用程序.有关更多详细信息,请参阅常见问题解答:https: //github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular 在等待带定位符的元素时 - 定位器:By(css选择器,.工具栏标题)
根据这个问题(https://github.com/angular/protractor/issues/3349)我需要使用NgZone在Angular之外运行一个Observable区间.我尝试了不同的组合this.ngZone.runOutsideAngular()
但没有工作,测试继续超时.
例如,这不起作用:
@Effect() setSessionTimer$ = this.actions$
.ofType(Auth.ActionTypes.SET_SECONDS_LEFT)
.map(toPayload)
.switchMap(secondsLeft => this.ngZone.runOutsideAngular(() => Observable.concat(
Observable.timer((secondsLeft - 60) * 1000).map(_ => new Auth.SessionExpiringAction(60)),
Observable.timer(60 * 1000).map(_ => new Auth.SessionExpiredAction())
)));
Run Code Online (Sandbox Code Playgroud)
我不知道如何在Angular之外运行效果.有没有人成功测试过他们的ngrx应用程序?
Mik*_*e R 25
解决方案是安排计时器observable在NgZone之外运行,然后在发生有趣的事情时重新进入区域.
首先,您将需要两个实用程序函数来包装任何调度程序并使效果进入或离开区域:
import { Subscription } from 'rxjs/Subscription';
import { Scheduler } from 'rxjs/Scheduler';
import { NgZone } from '@angular/core';
class LeaveZoneSchduler {
constructor(private zone: NgZone, private scheduler: Scheduler) { }
schedule(...args: any[]): Subscription {
return this.zone.runOutsideAngular(() =>
this.scheduler.schedule.apply(this.scheduler, args)
);
}
}
class EnterZoneScheduler {
constructor(private zone: NgZone, private scheduler: Scheduler) { }
schedule(...args: any[]): Subscription {
return this.zone.run(() =>
this.scheduler.schedule.apply(this.scheduler, args)
);
}
}
export function leaveZone(zone: NgZone, scheduler: Scheduler): Scheduler {
return new LeaveZoneSchduler(zone, scheduler) as any;
}
export function enterZone(zone: NgZone, scheduler: Scheduler): Scheduler {
return new EnterZoneScheduler(zone, scheduler) as any;
}
Run Code Online (Sandbox Code Playgroud)
然后使用调度程序(如asap
或async
),您可以使流进入或离开区域:
import { async } from 'rxjs/scheduler/async';
import { enterZone, leaveZone } from './util';
actions$.ofType('[Light] Turn On')
.bufferTime(300, leaveZone(this.ngZone, async))
.filter(messages => messages.length > 0)
.observeOn(enterZone(this.ngZone, async))
Run Code Online (Sandbox Code Playgroud)
请注意,大多数基于时间的运营商(像bufferTime
,debounceTime
,Observable.timer
等等)已经接受的替代调度.observeOn
当有趣的事情发生时,您只需要重新进入区域.
moh*_*ndo 10
对于Angular 6和RxJS 6,请使用以下代码:
import { SchedulerLike, Subscription } from 'rxjs'
import { NgZone } from '@angular/core'
class LeaveZoneScheduler implements SchedulerLike {
constructor(private zone: NgZone, private scheduler: SchedulerLike) { }
schedule(...args: any[]): Subscription {
return this.zone.runOutsideAngular(() =>
this.scheduler.schedule.apply(this.scheduler, args)
)
}
now (): number {
return this.scheduler.now()
}
}
class EnterZoneScheduler implements SchedulerLike {
constructor(private zone: NgZone, private scheduler: SchedulerLike) { }
schedule(...args: any[]): Subscription {
return this.zone.run(() =>
this.scheduler.schedule.apply(this.scheduler, args)
)
}
now (): number {
return this.scheduler.now()
}
}
export function leaveZone(zone: NgZone, scheduler: SchedulerLike): SchedulerLike {
return new LeaveZoneScheduler(zone, scheduler)
}
export function enterZone(zone: NgZone, scheduler: SchedulerLike): SchedulerLike {
return new EnterZoneScheduler(zone, scheduler)
}
Run Code Online (Sandbox Code Playgroud)
效果应如下所示:
import { asyncScheduler } from 'rxjs'
import { filter, observeOn, bufferTime } from 'rxjs/operators'
import { enterZone, leaveZone } from './util';
actions$.ofType('[Light] Turn On')
.pipe(
bufferTime(300, leaveZone(this.ngZone, asyncScheduler)),
filter(messages => messages.length > 0),
observeOn(enterZone(this.ngZone, asyncScheduler)),
)
Run Code Online (Sandbox Code Playgroud)