gpt4 book ai didi

c# - ReactiveUI:为什么在使用 TestScheduler 时必须在 "...Throttle..."中明确指定调度程序

转载 作者:行者123 更新时间:2023-11-30 16:50:14 27 4
gpt4 key购买 nike

我是 ReactiveUI 的新手。我有以下简单设置:可以指定 csv 的路径,并将包含的数据点显示给用户(使用 oxyplot)。现在我正在尝试测试以下订阅:

public GraphViewModel(IScreen hostScreen)
{
HostScreen = hostScreen;

setupGraphFormatting();

// Data Loading if path is valid
this.WhenAnyValue(viewModel => viewModel.PathToDataCsv)
.ObserveOn(RxApp.MainThreadScheduler)
.Throttle(TimeSpan.FromMilliseconds(500), RxApp.TaskpoolScheduler)
.Select(csvPath => csvPath?.Trim('"'))
.Where(csvPath => !string.IsNullOrEmpty(csvPath) && File.Exists(csvPath))
.Subscribe(csvPath =>
{
csvPath = csvPath?.Trim('"');
updatePlotModel(csvPath);
}, exception => {});

/* additional Code*/
}

这是相应的单元测试:

[Test]
public void If_PathToDataCsv_has_a_valid_value()
{
new TestScheduler().With(scheduler =>
{
string pathToValidCsvFile = "data.log";
var viewModel = new GraphViewModel(null);

scheduler.AdvanceByMs(1000);

viewModel.PathToDataCsv = pathToValidCsvFile;

scheduler.AdvanceByMs(1000);

viewModel.PlotModel.Series.Count.Should().Be(6);
});
}

我的 WhenAnyValue 的第一个实现没有专门设置任何调度程序(在 Throttle 中并且缺少任何 ObserverOn ):

public GraphViewModel(IScreen hostScreen)
{
HostScreen = hostScreen;

setupGraphFormatting();

// Data Loading if path is valid
this.WhenAnyValue(viewModel => viewModel.PathToDataCsv)
.Throttle(TimeSpan.FromMilliseconds(500))
.Select(csvPath => csvPath?.Trim('"'))
.Where(csvPath => !string.IsNullOrEmpty(csvPath) && File.Exists(csvPath))
.Subscribe(csvPath =>
{
csvPath = csvPath?.Trim('"');
updatePlotModel(csvPath);
}, exception => {});

/* additional Code*/
}

但是后来我的单元测试失败了。我的假设是 TestScheduler 在幕后被用于 Throttle,我不需要做任何事情。我做错了什么或者这是正确的方法:如果我想使用 TestScheduler/TimeTravel™,我必须按照我的方式指定调度程序?

编辑以回应 Glenn Watsons 的回答:好的,现在很清楚了:有问题的方法(Throttle、ObserverOn)当然不使用 ReactiveUI 的 Schedulers,因为这些方法来自 Reactive Extensions Framework。因此,在单元测试的情况下,它们不能被 ReactiveUI 隐式替换,除非我告诉方法使用 RxApp 调度程序...

最佳答案

RxApp 在 Release模式下提供 ThreadPoolScheduler,在单元测试模式下提供测试调度程序。

默认情况下,响应式扩展(与 ReactiveUI 分开)将使用它们自己的默认调度程序,这些调度程序不知道单元测试。

关于c# - ReactiveUI:为什么在使用 TestScheduler 时必须在 "...Throttle..."中明确指定调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35338764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com