|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Reactive; |
| 6 | +using System.Reactive.Concurrency; |
| 7 | +using System.Reactive.Disposables; |
| 8 | +using System.Reactive.Linq; |
| 9 | +using System.Threading; |
| 10 | +using Microsoft.Reactive.Testing; |
| 11 | + |
| 12 | +namespace FluentAssertions.Reactive |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Observer for testing <see cref="Observable"/>s using the FluentAssertions framework |
| 16 | + /// </summary> |
| 17 | + /// <typeparam name="TPayload"></typeparam> |
| 18 | + public class FluentTestObserver<TPayload> : IObserver<TPayload>, IDisposable |
| 19 | + { |
| 20 | + private readonly IDisposable _subscription; |
| 21 | + private readonly IScheduler _observeScheduler; |
| 22 | + private readonly RollingReplaySubject<Recorded<Notification<TPayload>>> _rollingReplaySubject = new RollingReplaySubject<Recorded<Notification<TPayload>>>(); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The observable which is observed by this instance |
| 26 | + /// </summary> |
| 27 | + public IObservable<TPayload> Subject { get; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The stream of recorded <see cref="Notification{T}"/>s |
| 31 | + /// </summary> |
| 32 | + public IObservable<Recorded<Notification<TPayload>>> RecordedNotificationStream => _rollingReplaySubject.AsObservable(); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The recorded <see cref="Notification{T}"/>s |
| 36 | + /// </summary> |
| 37 | + public IEnumerable<Recorded<Notification<TPayload>>> RecordedNotifications => |
| 38 | + _rollingReplaySubject.GetSnapshot(); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The recorded messages |
| 42 | + /// </summary> |
| 43 | + public IEnumerable<TPayload> RecordedMessages => |
| 44 | + RecordedNotifications.GetMessages(); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// The exception |
| 48 | + /// </summary> |
| 49 | + public Exception Error => |
| 50 | + RecordedNotifications |
| 51 | + .Where(r => r.Value.Kind == NotificationKind.OnError) |
| 52 | + .Select(r => r.Value.Exception) |
| 53 | + .FirstOrDefault(); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// The recorded messages |
| 57 | + /// </summary> |
| 58 | + public bool Completed => |
| 59 | + RecordedNotifications |
| 60 | + .Any(r => r.Value.Kind == NotificationKind.OnCompleted); |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Creates a new <see cref="FluentTestObserver{TPayload}"/> which subscribes to the supplied <see cref="IObservable{T}"/> |
| 64 | + /// </summary> |
| 65 | + /// <param name="subject">the <see cref="IObservable{T}"/> under test</param> |
| 66 | + public FluentTestObserver(IObservable<TPayload> subject) |
| 67 | + { |
| 68 | + Subject = subject; |
| 69 | + _observeScheduler = new EventLoopScheduler(); |
| 70 | + _subscription = new CompositeDisposable(); subject.ObserveOn(_observeScheduler).Subscribe(this); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Creates a new <see cref="FluentTestObserver{TPayload}"/> which subscribes to the supplied <see cref="IObservable{T}"/> |
| 75 | + /// </summary> |
| 76 | + /// <param name="subject">the <see cref="IObservable{T}"/> under test</param> |
| 77 | + public FluentTestObserver(IObservable<TPayload> subject, IScheduler scheduler) |
| 78 | + { |
| 79 | + Subject = subject; |
| 80 | + _observeScheduler = scheduler; |
| 81 | + _subscription = subject.ObserveOn(scheduler).Subscribe(this); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Creates a new <see cref="FluentTestObserver{TPayload}"/> which subscribes to the supplied <see cref="IObservable{T}"/> |
| 86 | + /// </summary> |
| 87 | + /// <param name="subject">the <see cref="IObservable{T}"/> under test</param> |
| 88 | + public FluentTestObserver(IObservable<TPayload> subject, TestScheduler testScheduler) |
| 89 | + { |
| 90 | + Subject = subject; |
| 91 | + _observeScheduler = testScheduler; |
| 92 | + _subscription = subject.ObserveOn(Scheduler.CurrentThread).Subscribe(this); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Clears the recorded notifications and messages as well as the recorded notifications stream buffer |
| 97 | + /// </summary> |
| 98 | + public void Clear() => _rollingReplaySubject.Clear(); |
| 99 | + |
| 100 | + /// <inheritdoc /> |
| 101 | + public void OnNext(TPayload value) |
| 102 | + { |
| 103 | + _rollingReplaySubject.OnNext( |
| 104 | + new Recorded<Notification<TPayload>>(_observeScheduler.Now.UtcTicks, Notification.CreateOnNext(value))); |
| 105 | + } |
| 106 | + |
| 107 | + /// <inheritdoc /> |
| 108 | + public void OnError(Exception exception) => |
| 109 | + _rollingReplaySubject.OnNext(new Recorded<Notification<TPayload>>(_observeScheduler.Now.UtcTicks, Notification.CreateOnError<TPayload>(exception))); |
| 110 | + |
| 111 | + /// <inheritdoc /> |
| 112 | + public void OnCompleted() => |
| 113 | + _rollingReplaySubject.OnNext(new Recorded<Notification<TPayload>>(_observeScheduler.Now.UtcTicks, Notification.CreateOnCompleted<TPayload>())); |
| 114 | + |
| 115 | + /// <inheritdoc /> |
| 116 | + public void Dispose() |
| 117 | + { |
| 118 | + _subscription?.Dispose(); |
| 119 | + _rollingReplaySubject?.Dispose(); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Returns an <see cref="ReactiveAssertions{TPayload}"/> object that can be used to assert the observed <see cref="IObservable{T}"/> |
| 124 | + /// </summary> |
| 125 | + /// <returns></returns> |
| 126 | + public ReactiveAssertions<TPayload> Should() => new ReactiveAssertions<TPayload>(this); |
| 127 | + } |
| 128 | +} |
0 commit comments