Add a extension .Subject() on Task<AndWhichConstraint<...>> to return Task<TMatchedElement>
See original GitHub issueAdd a extension .Subject() Task<AndWhichConstraint<…>> to return Task<TMatchedElement>
If I have an async assertion such as .ShouldNotThrowAsync()
and I want to access and store the subject of the assertion, I need to await
the assertion in brackets and then access the .Subject
, which can feel a little clumsy and hard to read with brackets extending across several lines.
Instead, if I had an extension method .Subject()
on Task<AndWhichConstraint<TParentConstraint, TMatchedElement>>
which returned Task<TMatchedElement>
then I could access the subject fluently as a task and await that instead.
Hope this sounds useful - I’m happy to contribute this change if it meets with approval!
An assertion that I could write today:
var version = (await new HttpClient().Invoking(
async httpClient => await httpClient.GetStringAsync("https://some.url/version"))
.Should()
.NotThrowAsync()).Subject;
(note the brackets enclosing four lines of code)
The extension method I propose adding:
public static class AssertionTaskExtensions
{
public static async Task<TMatchedElement> Subject<TParentConstraint, TMatchedElement>(
this Task<AndWhichConstraint<TParentConstraint, TMatchedElement>> assertions) => (await assertions).Subject;
}
The original assertion, rewritten to use this extension method:
var version = await new HttpClient().Invoking(
async httpClient => await httpClient.GetStringAsync("https://some.url/version"))
.Should()
.NotThrowAsync().Subject();
(note the brackets are no longer needed)
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jmg48 The .NET community is somewhat split these days on whether async methods can, should or must be suffixed with
Async
.In my own tests these days, I tend to use
NotThrow
less and less, and write something likeFor inspiration, we do have support for writing
Okay, yes I appreciate the complexity of choosing the right name and then being stuck with it.
For me, the reason to call it Subject is because I have a task which, were I to await it, I could then access the Subject property and get the same thing as if I use the proposed Subject extension method and then await that. The two routes lead to the same thing.
On Sat, 23 Jul 2022, 16:44 Lukas Grützmacher, @.***> wrote: