From f639b7081b8b425b72008e247ad7e501f1c30d92 Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Tue, 26 Apr 2022 15:01:50 +0200 Subject: [PATCH] Fix the failure message for occurrence regex --- Src/FluentAssertions/Primitives/StringAssertions.cs | 5 +++-- .../Primitives/StringAssertionSpecs.MatchRegex.cs | 6 +++--- docs/_pages/releases.md | 3 +++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Src/FluentAssertions/Primitives/StringAssertions.cs b/Src/FluentAssertions/Primitives/StringAssertions.cs index a55ad0236d..613360256c 100644 --- a/Src/FluentAssertions/Primitives/StringAssertions.cs +++ b/Src/FluentAssertions/Primitives/StringAssertions.cs @@ -507,8 +507,9 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus .ForConstraint(occurrenceConstraint, actual) .UsingLineBreaks .BecauseOf(because, becauseArgs) - .FailWith($"Expected {{context:string}} to match regex {{0}} {{expectedOccurrence}}{{reason}}, but found it {actual.Times()}.", - regexStr); + .FailWith($"Expected {{context:string}} to match regex {{0}} {{expectedOccurrence}}{{reason}}, " + + $"but found it {actual.Times()} in {{1}}.", + regexStr, Subject); } return new AndConstraint((TAssertions)this); diff --git a/Tests/FluentAssertions.Specs/Primitives/StringAssertionSpecs.MatchRegex.cs b/Tests/FluentAssertions.Specs/Primitives/StringAssertionSpecs.MatchRegex.cs index e84f9342aa..6c62af2589 100644 --- a/Tests/FluentAssertions.Specs/Primitives/StringAssertionSpecs.MatchRegex.cs +++ b/Tests/FluentAssertions.Specs/Primitives/StringAssertionSpecs.MatchRegex.cs @@ -225,7 +225,7 @@ public void When_a_string_is_matched_and_the_count_of_matches_do_not_fit_the_exp // Assert act.Should().Throw() - .WithMessage($"Expected subject to match regex*\"Lorem.*\" exactly 2 times, but found it 1 time."); + .WithMessage($"Expected subject to match regex*\"Lorem.*\" exactly 2 times, but found it 1 time in *Lorem*"); } [Fact] @@ -252,7 +252,7 @@ public void When_a_string_is_matched_and_the_expected_count_is_zero_and_string_m // Assert act.Should().Throw() - .WithMessage($"Expected subject to match regex*\"a\" exactly 0 times, but found it 1 time."); + .WithMessage($"Expected subject to match regex*\"a\" exactly 0 times, but found it 1 time in *{subject}*"); } [Fact] @@ -305,7 +305,7 @@ public void When_the_subject_is_empty_and_expected_count_is_more_than_zero_it_fa // Assert act.Should().Throw() - .WithMessage($"Expected subject to match regex* at least 1 time, but found it 0 times.*"); + .WithMessage($"Expected subject to match regex* at least 1 time, but found it 0 times*"); } [Fact] diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index 3011877865..0af73fa194 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -16,6 +16,9 @@ sidebar: * Add `For`/`Exclude` to allow exclusion of members inside a collection - [#1782](https://github.com/fluentassertions/fluentassertions/pull/1782) * Added overload for `HaveElement` for `XDocument` and `XElement` to assert on number of XML nodes - [#1880](https://github.com/fluentassertions/fluentassertions/pull/1880) +### Fixes +* Fix the failure message for regex matches (occurrence overload) to include the missing subject - [#1913](https://github.com/fluentassertions/fluentassertions/pull/1913) + ## 6.6.0 ### What's New