shouldly / shouldly

Should testing for .NET—the way assertions should be!

Home Page:https://docs.shouldly.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature suggestion: Explicit Task<T> support for Should*() methods?

martincostello opened this issue · comments

I've recently migrated some UI tests from Selenium to Playwright, with the main difference between the two being that Playwright is pretty much exclusively async for all of its methods for accessing elements in the DOM. This means that async has spread throughout the test suite, including through page object models.

With the use of Shouldly, this has turned code that used to be like this:

// Act
page.ClickImportButton();

// Assert
page.IsMapDisplayed().ShouldBeTrue();

into code like this:

await page.ClickImportButtonAsync();

// Assert
(await page.IsMapDisplayedAsync()).ShouldBeTrue();

The need for the extra parentheses is a bit messy and annoying, but isn't as bad as putting everything into a local variable first before doing the assert.

I was wondering if there was any mileage/value in adding some overloads to some of the more common Should*() methods that would extend Task<T> where the extension would await the task and then perform the assert.

The resulting code would end up something like this:

await page.IsMapDisplayedAsync().ShouldBeTrue();

// or:
await page.IsMapDisplayedAsync().ShouldBeTrueAsync();

with the implementation being something like this:

public static async Task ShouldBeTrue(this Task<bool> task)
{
    bool actual = await task;
    actual.ShouldBeTrue();
}