-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReactiveAssertionSpecs.cs
225 lines (186 loc) · 8.29 KB
/
ReactiveAssertionSpecs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using FluentAssertions.Formatting;
using Microsoft.Reactive.Testing;
using Xunit;
using Xunit.Sdk;
namespace FluentAssertions.Reactive.Specs
{
[Collection("Reactive")]
public class ReactiveAssertionSpecs: ReactiveTest
{
[Fact]
public void When_the_expected_number_of_notifications_where_pushed_it_should_not_throw()
{
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnNext(100, 1),
OnNext(200, 2),
OnNext(300, 3));
// observe the sequence
using var observer = observable.Observe(scheduler);
// push subscriptions
scheduler.AdvanceTo(400);
// Act
Action act = () => observer.Should().Push(3);
// Assert
act.Should().NotThrow();
observer.RecordedNotifications.Should().BeEquivalentTo(observable.Messages);
}
[Fact]
public void When_the_expected_number_of_notifications_where_not_pushed_it_should_throw()
{
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnNext(100, 1),
OnNext(200, 2),
OnNext(300, 3));
// observe the sequence
using var observer = observable.Observe(scheduler);
// assert a single notification
// Act
Action act = () => observer.Should().Push(1, TimeSpan.FromMilliseconds(1));
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to push at least 1 notification, but 0 were received within {Formatter.ToString(TimeSpan.FromMilliseconds(1))}.");
observer.RecordedNotifications.Should().BeEmpty("because no messages have been pushed");
// assert multiple notifications
scheduler.AdvanceTo(250);
// Act
act = () => observer.Should().Push(3, TimeSpan.FromMilliseconds(1));
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to push at least 3 notifications, but 2 were received within {Formatter.ToString(TimeSpan.FromMilliseconds(1))}.");
observer.RecordedNotifications.Should().BeEquivalentTo(observable.Messages.Take(2));
}
[Fact]
public void When_the_observable_fails_instead_of_pushing_notifications_it_should_throw()
{
var exception = new ArgumentException("That was wrong.");
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnError<Unit>(1, exception));
// observe the sequence
using var observer = observable.Observe(scheduler);
scheduler.AdvanceTo(10);
// Act
Action act = () => observer.Should().Push();
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to push at least 1 notification, but it failed with a {Formatter.ToString(exception)}.");
observer.Error.Should().BeEquivalentTo(exception);
}
[Fact]
public void When_the_observable_fails_as_expected_it_should_not_throw()
{
var exception = new ArgumentException("That was wrong.");
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnError<Unit>(1, exception));
// observe the sequence
using var observer = observable.Observe(scheduler);
scheduler.AdvanceTo(10);
// Act
Action act = () => observer.Should().Throw<ArgumentException>()
.WithMessage(exception.Message);
// Assert
act.Should().NotThrow();
observer.Error.Should().BeEquivalentTo(exception);
}
[Fact]
public void When_the_observable_is_expected_to_fail_but_does_not_it_should_throw()
{
var exception = new ArgumentException("That was wrong.");
var observable = Observable.Return(false);
// observe the sequence
using var observer = observable.Observe();
// Act
Action act = () => observer.Should().Throw<ArgumentException>(TimeSpan.FromMilliseconds(1));
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to throw a <System.ArgumentException>, but no exception was thrown.");
observer.Error.Should().BeNull();
}
[Fact]
public void When_the_observable_completes_as_expected_it_should_not_throw()
{
var observable = Observable.Empty<Unit>();
// observe the sequence
using var observer = observable.Observe();
// Act
Action act = () => observer.Should().Complete();
// Assert
act.Should().NotThrow();
}
[Fact]
public void When_the_observable_is_expected_to_complete_but_does_not_it_should_throw()
{
var exception = new ArgumentException("That was wrong.");
var observable = new Subject<Unit>();
// observe the sequence
using var observer = observable.Observe();
// Act
Action act = () => observer.Should().Complete(TimeSpan.FromMilliseconds(1));
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to complete within {Formatter.ToString(TimeSpan.FromMilliseconds(1))}, but it did not.");
observer.Error.Should().BeNull();
}
[Fact]
public void When_the_observable_pushes_an_expected_match_it_should_not_throw()
{
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnNext(100, 1),
OnNext(200, 2));
// observe the sequence
using var observer = observable.Observe(scheduler);
// push subscriptions
scheduler.AdvanceTo(250);
// Act
Action act = () => observer.Should().PushMatch(i => i > 1);
// Assert
act.Should().NotThrow();
observer.RecordedNotifications.Should().BeEquivalentTo(observable.Messages);
}
[Fact]
public void When_the_observable_does_not_push_a_match_it_should_throw()
{
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnNext(100, 1),
OnNext(200, 2));
// observe the sequence
using var observer = observable.Observe(scheduler);
// push subscriptions
scheduler.AdvanceTo(250);
// Act
Action act = () => observer.Should().PushMatch(i => i > 3, TimeSpan.FromMilliseconds(1));
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to push an item matching (i > 3) within {Formatter.ToString(TimeSpan.FromMilliseconds(1))}.");
observer.RecordedNotifications.Should().BeEquivalentTo(observable.Messages);
}
[Fact]
public void When_the_observable_fails_instead_of_pushing_a_match_it_should_throw()
{
var exception = new ArgumentException("That was wrong.");
var scheduler = new TestScheduler();
var observable = scheduler.CreateColdObservable(
OnError<int>(1, exception));
// observe the sequence
using var observer = observable.Observe(scheduler);
scheduler.AdvanceTo(10);
// Act
Action act = () => observer.Should().PushMatch(i => i > 1);
// Assert
act.Should().Throw<XunitException>().WithMessage(
$"Expected observable to push an item matching (i > 1), but it failed with a {Formatter.ToString(exception)}.");
observer.Error.Should().BeEquivalentTo(exception);
}
}
}