I have this class:
public class TestService
{
public IObservable<int> GetObservable(int max)
{
var subject = new Subject<int>();
Task.Factory.StartNew(() =>
{
for (int i = 0; i < max; i++)
{
subject.OnNext(i);
}
subject.OnCompleted();
});
return subject;
}
}
I wrote a test method for this as well:
[TestMethod]
public void TestServiceTest1()
{
var testService = new TestService();
var i = 0;
var observable = testService.GetObservable(3);
observable.Subscribe(_ =>
{
i++;
});
observable.Wait();
Assert.AreEqual(i, 3);
}
But sometimes I get the error: Sequence contains no elements in method Wait().
I propose that my IObservable is completed before test reaches the observable.Wait() line. How can I avoid this error?