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

Variable name is not captured after await assertion #1770

Merged
merged 3 commits into from Jan 11, 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
10 changes: 4 additions & 6 deletions Src/FluentAssertions/CallerIdentifier.cs
Expand Up @@ -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++)
{
Expand Down
16 changes: 16 additions & 0 deletions Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs
Expand Up @@ -452,6 +452,22 @@ public void When_the_method_has_Should_prefix_it_should_read_whole_method()
act.Should().Throw<XunitException>()
.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> task = async () => await Task.Yield();

// Act
await task.Should().NotThrowAsync();
Action act = () => someText.Should().Be("Hi");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("*someText*", "it should capture the variable name");
}
}

[SuppressMessage("The name of a C# element does not begin with an upper-case letter", "SA1300")]
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Expand Up @@ -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

Expand Down