I have a problem when trying to unit test a view model that uses WhenAnyValue to listen for changes in a property, and populate a list based on the new value of the property. I need to use Throttle when listening to changes on the property due to interaction with 3'rd party software.
My solution works in production, but I have some trouble with my unit test. It seems to be related to not getting the test scheduler to advance correctly, so that the subscription after the throttle is actually run. I have created a simplified version of my problem which I hope is illustrative.
View model to test
public class ViewModel : ReactiveObject
{
public ViewModel(IScheduler scheduler)
{
ListToBeFilled = new List<int>();
this.WhenAnyValue(vm => vm.SelectedValue)
.Throttle(TimeSpan.FromMilliseconds(500), scheduler)
.Skip(1)
.Subscribe(value =>
{
// Do a computation based on value and store result in
// ListToBeFilled.
ListToBeFilled = new List<int>() {1, 2, 3};
});
}
private string _selectedValue;
public string SelectedValue
{
get { return _selectedValue; }
set { this.RaiseAndSetIfChanged(ref _selectedValue, value); }
}
public List<int> ListToBeFilled { get; set; }
}
View model test
[TestFixture]
[RequiresSTA]
public class ViewModelTests
{
[Test]
public void TestViewModel()
{
// Arrange
(new TestScheduler()).With(scheduler =>
{
ViewModel vm = new ViewModel(scheduler);
// Act
vm.SelectedValue = "test value";
scheduler.AdvanceByMs(1000);
// Assert
Assert.AreEqual(3, vm.ListToBeFilled.Count);
});
}
}
The test fails saying Expected: 3 But was 0. When running the test without using Throttle (which I need to get things to work in production), the test passes. Am I using the test scheduler wrong? What do I need to do in order for the Throttle to be consumed?