Partly this comes down to the documentation for TestScheduler just not being very helpful, but...
I have a class which my DI framework provides as a singleton:
public class Foo : IObservable<Bar>
{
private readonly IObservable<Bar> _feed;
private readonly IBarSource _remote;
public Foo(IBarSource remote, IScheduler scheduler)
{
_remote = remote;
_feed = Observable.FromAsync(async token => GetBarsAsync(token))
.Merge()
.Repeat()
.RefCount()
.ObserveOn(scheduler);
}
public IDisposable Subscribe(IObserver<Bar> observer)
{
retu _feed.Subscribe(observer);
}
private async Task<List<Bar>> GetBarsAsync(CancellationToken token)
{
var raw = await _remote.GetRawBarsAsync();
token.ThrowIfCancellationRequested();
retu raw.Select(ProcessBars).ToList();
}
}
Basically it tus a data stream that has to be polled into a nice observable; having it as a singleton class means all the different components of the application that need data from this stream get it from a single source rather than each of them spiing up their own background thread to get it.
I'm using ObserveOn to make sure updates go out on the UI thread; I take in a scheduler for that so that the DI framework can provide DispatcherScheduler.Current in the actual application but I can provide a TestScheduler in unit tests so I can test the class...
...except I can't seem to figure out how to use the TestScheduler in such a way that events actually get published. I have this:
[Test]
public void BasicTest()
{
// arrange
var remote = Mock.Of<IBarSource>();
var sched = new TestScheduler();
var instance = new Foo(remote, sched);
// act
bool published = false;
var sub = instance.Subscribe(_ => published = true);
sched.Start();
sub.Dispose();
// assert
published.Should().BeTrue();
}
But it always fails. I assume I'm not using the TestScheduler correctly but all the examples I can find seem to assume you're using it to control periodic events rather than using it in ObserveOn. What am I doing wrong?
