我有一个ReactiveList,其视图模型带有'Selected'属性。当我尝试从单元测试中设置此属性时,不会调用ReactiveList.ItemChanged上的订阅。但是,它在运行应用程序并从UI中的复选框触发“Selected”属性时起作用。为了使ReactiveList.ItemChanged在单元测试环境中工作,我还有什么魔力吗?我顺便使用ReactiveUI 6.5。
WellsViewModels = new ReactiveList<WellViewModel> { ChangeTrackingEnabled = true };
var selectedWellsObservable =
Observable.Merge(
this.WhenAnyValue(vm => vm.WellSamplesFunc)
.Select(_ => Enumerable.Empty<IPropertyModelingWell>()),
WellsViewModels
.ItemChanged
.Where(x => x.PropertyName == nameof(WellViewModel.Selected))
.Throttle(TimeSpan.FromMilliseconds(100), RxApp.MainThreadScheduler)
.Select(_ => WellsViewModels.Where(vm => vm.Selected)
.Select(vm => vm.Well).ToList())
)
.Publish()
.RefCount();
selectedWellsObservable
.Select(_ => ComputeAllCheckedState(WellsViewModels))
.Subscribe(b => CheckAllWells = b) // <--- This is never invoked when unit-testing!?!
[Test]
public void CheckAllWells_UpdatedWhenWellsViewModelsSelectionChanges()
{
new TestScheduler().With(scheduler =>
{
// Arrange
...
...
// Act
_viewModel.WellsViewModels[0].Selected = true;
scheduler.AdvanceBy(TimeSpan.FromMilliseconds(200).Ticks);
// Assert
Assert.IsFalse(_viewModel.CheckAllWells.HasValue);
});
}