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 2 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/Specialized/TaskAssertionSpecs.cs
Expand Up @@ -143,5 +143,21 @@ async Task CheckContextAsync()
SynchronizationContext.Current.Should().NotBeNull();
}
}

[UIFact]
Copy link
Member

Choose a reason for hiding this comment

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

🤔 This test belongs in the CallerIdentifierSpecs
🔧 I think rephrasing this test to Caller_identification_should_also_work_for_statements_following_async_code will make the purpose of the test much clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in cde578c.

public async Task When_resolving_async_to_main_thread_variable_name_used_for_assertion_should_be_detected()
{
// 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");
}
}
}
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