Skip to content

Instantly share code, notes, and snippets.

@KevM
Created October 12, 2015 18:35
Show Gist options
  • Save KevM/6a0bdbf77907cd853fb6 to your computer and use it in GitHub Desktop.
Save KevM/6a0bdbf77907cd853fb6 to your computer and use it in GitHub Desktop.
IObservable extensions for testing
public static class ObservableExtensions
{
public static Func<bool> WasObserved<T>(this IObservable<T> o, T expected)
{
var wasExpectedObserved = false;
var result = o.Subscribe(s =>
{
wasExpectedObserved = s.Equals(expected);
});
return () =>
{
result.Dispose();
return wasExpectedObserved;
};
}
public static Func<IEnumerable<T>> CaptureObserved<T>(this IObservable<T> o)
{
var result = new List<T>();
var sub = o.Subscribe(s =>
{
result.Add(s);
});
return () =>
{
sub.Dispose();
return result;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment