Skip to content

Commit

Permalink
Variable name is not captured after await assertion (#1770)
Browse files Browse the repository at this point in the history
  • Loading branch information
MullerWasHere committed Jan 11, 2022
1 parent 305b36a commit 96a8fc7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
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

0 comments on commit 96a8fc7

Please sign in to comment.