【发布时间】:2016-04-18 17:50:09
【问题描述】:
我创建了以下辅助方法来帮助测试 observables:
public static void ExecuteObservableTest<T>(IObservable<T> observable, Action action, Action assert, bool expectTimeout, int timeout = 500)
{
Exception ex = null;
var scheduler = new TestScheduler();
observable.Timeout(TimeSpan.FromMilliseconds(timeout)).ObserveOn(scheduler).Subscribe(_ => { }, e => ex = e);
action();
scheduler.AdvanceBy(TimeSpan.FromMilliseconds(timeout).Ticks + 1);
assert();
if (expectTimeout) Assert.IsNotNull(ex);
else Assert.IsNull(ex);
}
只要我不期待超时,一切都会很好。对于我确实期望超时的测试用例,此方法永远不会到达 OnException 方法。使用 TestScheduler 实现此目的的正确方法是什么?
我还应该注意,超时是软超时,这意味着如果 observable 没有在分配的时间内完成,它永远不会完成 - 但 observable 本身没有定义相应的超时。
【问题讨论】:
标签: c# unit-testing system.reactive scheduler reactive-programming