Unit test IObservable <T> with ObserveOnDispatcher
I need to check the code snippet
var watcher = new FakeIFileSystemWatcher();
watcher.FilesToBeImported
.ObserveOnDispatcher()
.Subscribe(list.Add);
so I created this little unit test, but I cannot get it to list the reasons. Count is always 0
[Test]
public void Foo()
{
var list = new List<string>();
var watcher = new FakeIFileSystemWatcher();
watcher.FilesToBeImported
.ObserveOnDispatcher()
.Subscribe(list.Add);
Task task = Task.Factory.StartNew(() =>
{
watcher.AddFile("cc");
watcher.AddFile("cc");
watcher.AddFile("cc");
}, TaskCreationOptions.LongRunning);
Task.WaitAll(task);
Assert.AreEqual(3, list.Count);
}
if I comment on the method
.ObserveOnDispatcher()
it passes, but how can I make it work also with ObserveOnDispatcher ()?
+5
3 answers
I decided to use the DispatcherUtil class that I found here Using WPF Manager in Unit Tests
now my code is next
[Test]
public void Foo()
{
var list = new List<string>();
var watcher = new FakeIFileSystemWatcher();
watcher.FilesToBeImported
.ObserveOnDispatcher()
.Subscribe(list.Add);
Task task = Task.Factory.StartNew(() =>
{
watcher.AddFile("cc");
watcher.AddFile("cc");
watcher.AddFile("cc");
watcher.AddFile("cc");
}, TaskCreationOptions.LongRunning);
Task.WaitAll(task);
DispatcherUtil.DoEvents();
Assert.AreEqual(4, list.Count);
}
and it works like a charm
+1