Skip to content

Commit 62530b5

Browse files
committed
test new assertions
1 parent 83eaadc commit 62530b5

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

Src/FluentAssertions.Reactive/ReactiveAssertions.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,6 @@ public AndConstraint<ReactiveAssertions<TPayload>> PushMatch(
275275
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
276276

277277
IList<TPayload> notifications = new List<TPayload>();
278-
AssertionScope assertion = Execute.Assertion
279-
.WithExpectation("Expected {context:observable} {0} to push an item matching {1}{reason}", Subject, predicate.Body)
280-
.BecauseOf(because, becauseArgs);
281278

282279
try
283280
{
@@ -297,12 +294,15 @@ public AndConstraint<ReactiveAssertions<TPayload>> PushMatch(
297294
{
298295
if (e is AggregateException aggregateException)
299296
e = aggregateException.InnerException;
300-
assertion.FailWith(", but it failed with a {0}.", e);
297+
Execute.Assertion
298+
.BecauseOf(because, becauseArgs)
299+
.FailWith("Expected {context:observable} to push an item matching {0}{reason}, but it failed with a {1}.", predicate.Body, e);
301300
}
302-
303-
assertion
301+
302+
Execute.Assertion
303+
.BecauseOf(because, becauseArgs)
304304
.ForCondition(notifications.Any())
305-
.FailWith(" within {0}.", timeout);
305+
.FailWith("Expected {context:observable} to push an item matching {0}{reason} within {1}.", predicate.Body, timeout);
306306

307307
return new AndConstraint<ReactiveAssertions<TPayload>>(this);
308308
}
@@ -339,9 +339,6 @@ public async Task<AndConstraint<ReactiveAssertions<TPayload>>> PushMatchAsync(
339339
throw new ArgumentNullException(nameof(predicate));
340340

341341
IList<TPayload> notifications = new List<TPayload>();
342-
AssertionScope assertion = Execute.Assertion
343-
.WithExpectation("Expected {context:observable} {0} to push an item matching {1}{reason}", Subject, predicate.Body)
344-
.BecauseOf(because, becauseArgs);
345342

346343
try
347344
{
@@ -360,12 +357,15 @@ public async Task<AndConstraint<ReactiveAssertions<TPayload>>> PushMatchAsync(
360357
{
361358
if (e is AggregateException aggregateException)
362359
e = aggregateException.InnerException;
363-
assertion.FailWith(", but it failed with a {0}.", e);
360+
Execute.Assertion
361+
.BecauseOf(because, becauseArgs)
362+
.FailWith("Expected {context:observable} to push an item matching {0}{reason}, but it failed with a {1}.", predicate.Body, e);
364363
}
365364

366-
assertion
365+
Execute.Assertion
366+
.BecauseOf(because, becauseArgs)
367367
.ForCondition(notifications.Any())
368-
.FailWith(" within {0}.", timeout);
368+
.FailWith("Expected {context:observable} to push an item matching {0}{reason} within {1}.", predicate.Body, timeout);
369369

370370
return new AndWhichConstraint<ReactiveAssertions<TPayload>, IEnumerable<TPayload>>(this, notifications);
371371
}

Tests/FluentAssertions.Reactive.Specs/ReactiveAssertionSpecs.cs

+44-3
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,12 @@ public void When_the_observable_pushes_an_expected_match_it_should_not_throw()
164164
var scheduler = new TestScheduler();
165165
var observable = scheduler.CreateColdObservable(
166166
OnNext(100, 1),
167-
OnNext(200, 2),
168-
OnNext(300, 3));
167+
OnNext(200, 2));
169168

170169
// observe the sequence
171170
using var observer = observable.Observe(scheduler);
172171
// push subscriptions
173-
scheduler.AdvanceTo(400);
172+
scheduler.AdvanceTo(250);
174173

175174
// Act
176175
Action act = () => observer.Should().PushMatch(i => i > 1);
@@ -180,5 +179,47 @@ public void When_the_observable_pushes_an_expected_match_it_should_not_throw()
180179

181180
observer.RecordedNotifications.Should().BeEquivalentTo(observable.Messages);
182181
}
182+
183+
[Fact]
184+
public void When_the_observable_does_not_push_a_match_it_should_throw()
185+
{
186+
var scheduler = new TestScheduler();
187+
var observable = scheduler.CreateColdObservable(
188+
OnNext(100, 1),
189+
OnNext(200, 2));
190+
191+
// observe the sequence
192+
using var observer = observable.Observe(scheduler);
193+
// push subscriptions
194+
scheduler.AdvanceTo(250);
195+
196+
// Act
197+
Action act = () => observer.Should().PushMatch(i => i > 3, TimeSpan.FromMilliseconds(1));
198+
199+
// Assert
200+
act.Should().Throw<XunitException>().WithMessage(
201+
$"Expected observable to push an item matching (i > 3) within {Formatter.ToString(TimeSpan.FromMilliseconds(1))}.");
202+
203+
observer.RecordedNotifications.Should().BeEquivalentTo(observable.Messages);
204+
}
205+
206+
[Fact]
207+
public void When_the_observable_fails_instead_of_pushing_a_match_it_should_throw()
208+
{
209+
var exception = new ArgumentException("That was wrong.");
210+
var scheduler = new TestScheduler();
211+
var observable = scheduler.CreateColdObservable(
212+
OnError<int>(1, exception));
213+
214+
// observe the sequence
215+
using var observer = observable.Observe(scheduler);
216+
scheduler.AdvanceTo(10);
217+
// Act
218+
Action act = () => observer.Should().PushMatch(i => i > 1);
219+
// Assert
220+
act.Should().Throw<XunitException>().WithMessage(
221+
$"Expected observable to push an item matching (i > 1), but it failed with a {Formatter.ToString(exception)}.");
222+
observer.Error.Should().BeEquivalentTo(exception);
223+
}
183224
}
184225
}

0 commit comments

Comments
 (0)