question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variable name is not displayed after await assertion

See original GitHub issue

Description

It is found that the variable names will not be included in the assertion message when the assertion comes after the await assertion such as NotThrowAsync() and CompleteWithinAsync().

Complete minimal example reproducing the issue

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Extensions;

namespace MyUnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public async Task RunAsyncAssertion()
        {
            // Arrange and Act
            string someText = "Hello";
            Func<Task> myTask = () => Task.Delay(1.Seconds());

            // Assert
            await myTask.Should().CompleteWithinAsync(2.Seconds());
            someText.Should().Be("Hi");
        }
    }
}

Expected behavior:

Assertion failure should be something like Expected someText to be "Hi" with a length of 2, but "Hello" has a length of 5, differs near "ell" (index 1).

Actual behavior:

Expected string to be "Hi" with a length of 2, but "Hello" has a length of 5, differs near "ell" (index 1). Note that string is displayed instead of the variable name someText.

This is not caused by the await keyword used. If the Fluent async assertion is not used, the variable name can be displayed correctly. Example:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Extensions;

namespace MyUnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public async Task RunAsyncAssertion()
        {
            // Arrange and Act
            string someText = "Hello";
            Func<Task> myTask = () => Task.Delay(1.Seconds());

            // Assert
            await myTask.Invoke();
            someText.Should().Be("Hi");
        }
    }
}

Result: Expected someText to be "Hi" with a length of 2, but "Hello" has a length of 5, differs near "ell" (index 1).

Versions

  • Fluent Assertions 6.2.0
  • .NET framework 4.7.2

Additional Information

This is my first time submitting an issue on GitHub. Hope it’s alright. 😅

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
MullerWasHerecommented, Jan 6, 2022

@jnyrup I’d love to create a PR for it! Also, thanks for the suggestions.

1reaction
dennisdoomencommented, Dec 13, 2021

No, it should be compiled in debug mode. So that confirms it’s an actual bug in FA.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to show the variable name on the exception message ...
How to show the variable name on the exception message when the assertion fails using FluentAssertions?
Read more >
Assertion failure in async test case does not fail test overall
Don't use async in testcase like I specified in my previous post. It affects lot of areas like in assertion (doesn't assert properly...
Read more >
await - JavaScript - MDN Web Docs - Mozilla
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >
Assertions | Basic Guides
The following simple assertion compares variable x to variable y and succeeds if the two are equal: await t.expect(x).eql(y);. The left side of...
Read more >
Assertions
Assertions. Playwright includes test assertions in the form of expect function. To make an assertion, call expect(value) and choose a matcher that reflects ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found