Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More specifically clarify the intentions of WithArgs #1929

Merged
merged 1 commit into from May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you are not documenting these kinds of exceptions (contract exceptions) today with <exception elements. Have you considered doing so to improve the API documentation?

I know this is not directly related to this PR here (which I found by accident by following a link from the latest release notes), but since it is related to documentation, I figured I'd mention here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Occurrences:

  • <exception: 166
  • Guard.: 224

So we mostly (~75%) do guard calls to Guard., but sure, let's get that number closer to 100%.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh I see, thought this was indicative of the trend but didn't really check it on my end, thanks for clarifying!

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