diff --git a/Src/FluentAssertions/CallerIdentifier.cs b/Src/FluentAssertions/CallerIdentifier.cs index cbf3f2fa88..5dc0956a8b 100644 --- a/Src/FluentAssertions/CallerIdentifier.cs +++ b/Src/FluentAssertions/CallerIdentifier.cs @@ -40,13 +40,11 @@ public static string DetermineCallerIdentity() frame => !IsCurrentAssembly(frame)); } - int firstFluentAssertionsCodeIndex = Array.FindLastIndex( + int lastUserStackFrameBeforeFluentAssertionsCodeIndex = Array.FindIndex( allStackFrames, - searchStart, - frame => IsCurrentAssembly(frame)); - - int lastUserStackFrameBeforeFluentAssertionsCodeIndex = - firstFluentAssertionsCodeIndex + 1; + startIndex: 0, + count: searchStart + 1, + frame => !IsCurrentAssembly(frame) && !IsDotNet(frame)); for (int i = lastUserStackFrameBeforeFluentAssertionsCodeIndex; i < allStackFrames.Length; i++) { diff --git a/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs b/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs index 96098ebde3..a539f4f942 100644 --- a/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs +++ b/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs @@ -452,6 +452,22 @@ public void When_the_method_has_Should_prefix_it_should_read_whole_method() act.Should().Throw() .WithMessage("Expected foo.ShouldReturnSomeBool() to be false*"); } + + [UIFact] + public async Task Caller_identification_should_also_work_for_statements_following_async_code() + { + // Arrange + const string someText = "Hello"; + Func task = async () => await Task.Yield(); + + // Act + await task.Should().NotThrowAsync(); + Action act = () => someText.Should().Be("Hi"); + + // Assert + act.Should().Throw() + .WithMessage("*someText*", "it should capture the variable name"); + } } [SuppressMessage("The name of a C# element does not begin with an upper-case letter", "SA1300")] diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index 41a15a2547..833e2674ee 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -16,6 +16,7 @@ sidebar: ### Fixes * `ContainItemsAssignableTo` now expects at least one item assignable to `T` - [#1765](https://github.com/fluentassertions/fluentassertions/pull/1765) * Querying methods on classes, e.g. `typeof(MyController).Methods()`, now also includes static methods - [#1740](https://github.com/fluentassertions/fluentassertions/pull/1740) +* Variable name is not captured after await assertion - [#1770](https://github.com/fluentassertions/fluentassertions/pull/1770) ## 6.3.0