Skip to content

Commit

Permalink
Merge pull request #1929 from jnyrup/EventRaiseDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed May 21, 2022
2 parents e090a23 + 0262d64 commit 4053cab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
26 changes: 16 additions & 10 deletions Src/FluentAssertions/EventRaisingExtensions.cs
Expand Up @@ -14,9 +14,11 @@ namespace FluentAssertions
public static class EventRaisingExtensions
{
/// <summary>
/// Asserts that all occurrences of the event originated from the <param name="expectedSender"/> and
/// returns only the events that came from that sender.
/// Asserts that all occurrences of the event originates from the <param name="expectedSender"/>.
/// </summary>
/// <returns>
/// Returns only the events that comes from that sender.
/// </returns>
public static IEventRecording WithSender(this IEventRecording eventRecording, object expectedSender)
{
var eventsForSender = new List<OccurredEvent>();
Expand Down Expand Up @@ -53,10 +55,12 @@ public static IEventRecording WithSender(this IEventRecording eventRecording, ob
}

/// <summary>
/// Asserts that at least one occurence of the events had one or more arguments of the expected
/// type <typeparamref name="T"/> which matched the given predicate.
/// Returns only the events that matched both type and optionally a predicate.
/// Asserts that at least one occurrence of the events has some argument of the expected
/// type <typeparamref name="T"/> that matches the given predicate.
/// </summary>
/// <returns>
/// Returns only the events having some argument matching both type and predicate.
/// </returns>
public static IEventRecording WithArgs<T>(this IEventRecording eventRecording, Expression<Func<T, bool>> predicate)
{
Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate));
Expand All @@ -79,18 +83,20 @@ public static IEventRecording WithArgs<T>(this IEventRecording eventRecording, E

Execute.Assertion
.ForCondition(foundMatchingEvent)
.FailWith("Expected at least one event which arguments are of type <{0}> and matches {1}, but found none.",
.FailWith("Expected at least one event with some argument of type <{0}> that matches {1}, but found none.",
typeof(T),
predicate.Body);

return new FilteredEventRecording(eventRecording, eventsWithMatchingPredicate);
}

/// <summary>
/// Asserts that at least one occurence of the events had one or more arguments of the expected
/// type <typeparamref name="T"/> which matched the predicates in the same order.
/// Returns only the events that matched both type and optionally predicates.
/// Asserts that at least one occurrence of the events has arguments of the expected
/// type <typeparamref name="T"/> that pairwise match all the given predicates.
/// </summary>
/// <returns>
/// Returns only the events having arguments matching both type and all predicates.
/// </returns>
/// <remarks>
/// If a <c>null</c> is provided as predicate argument, the corresponding event parameter value is ignored.
/// </remarks>
Expand Down Expand Up @@ -128,7 +134,7 @@ public static IEventRecording WithArgs<T>(this IEventRecording eventRecording, p
if (!foundMatchingEvent)
{
Execute.Assertion
.FailWith("Expected at least one event which arguments are of type <{0}> and matches {1}, but found none.",
.FailWith("Expected at least one event with some arguments of type <{0}> that pairwise match {1}, but found none.",
typeof(T),
string.Join(" | ", predicates.Where(p => p is not null).Select(p => p.Body.ToString())));
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/FluentAssertions.Specs/Events/EventAssertionSpecs.cs
Expand Up @@ -179,7 +179,7 @@ public void When_the_event_parameters_dont_match_it_should_throw()
act
.Should().Throw<XunitException>()
.WithMessage(
"Expected at least one event which arguments are of type*PropertyChangedEventArgs*matches*(args.PropertyName == \"SomeProperty\"), but found none.");
"Expected at least one event with some argument of type*PropertyChangedEventArgs*matches*(args.PropertyName == \"SomeProperty\"), but found none.");
}

[Fact]
Expand Down Expand Up @@ -321,7 +321,7 @@ public void When_a_non_conventional_event_with_a_specific_argument_was_not_raise

// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected at least one event which arguments*type*Int32*matches*(args == " + wrongArgument + "), but found none.");
"Expected at least one event with some argument*type*Int32*matches*(args == " + wrongArgument + "), but found none.");
}

[Fact]
Expand All @@ -340,7 +340,7 @@ public void When_a_non_conventional_event_with_many_specific_arguments_was_not_r

// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected at least one event which arguments*matches*\"(args == \"" + wrongArgument +
"Expected at least one event with some arguments*match*\"(args == \"" + wrongArgument +
"\")\", but found none.");
}

Expand Down

0 comments on commit 4053cab

Please sign in to comment.