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

Verifying Method return value on async methods #228

Open
JanSdot opened this issue Feb 21, 2024 · 2 comments
Open

Verifying Method return value on async methods #228

JanSdot opened this issue Feb 21, 2024 · 2 comments
Labels
kind/support Categorizes issue or PR as a support question.

Comments

@JanSdot
Copy link

JanSdot commented Feb 21, 2024

Maybe I just don't know how to do it, but for me it seems that it is right now not possible to verify return parameters of a method, if that method is async, so actually return a Task<TReturn> where there Return is what you actually want to check.

It would be great if this would be added or a hint how this could be accomplished!

Great framework by the way!

@alexanderlinne alexanderlinne added the kind/support Categorizes issue or PR as a support question. label Mar 22, 2024
@carlreid
Copy link

carlreid commented Jun 13, 2024

I think I have achieved this the following way:

protected readonly IEnumerable<System.Type> AwaitableTypes = [typeof(Task), typeof(Task<>), typeof(ValueTask), typeof(ValueTask<>)];

/// <summary>
/// Methods returning <see cref="Task"/>, <see cref="Task{TResult}"/>, <see cref="ValueTask"/>, <see cref="ValueTask{TResult}"/>
/// should have names that end with "Async".
/// <see href="https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/task-asynchronous-programming-model#BKMK_NamingConvention">Naming Convention for Async Methods</see>
/// </summary>
[Fact]
public void Methods_ThatReturnAwaitableTypes_ShouldHaveAsyncSuffix()
{
    var rule = MethodMembers().That().HaveReturnType(AwaitableTypes)
         .Should().HaveNameContaining("Async(");

    rule.Check(Architecture);
}

I wanted to use .HaveNameEndingWith("Async");, but I think that might be checking the method name, plus arguments. For instance ExecuteAsync(bool something) is flagging up as a failure. Though with .HaveNameContaining("Async("); it passes.

@JanSdot
Copy link
Author

JanSdot commented Oct 30, 2024

I think there was a misunderstanding. I do not want to check that the method is named Async, but that it is returning a special type of type.

[Fact]
public void Methods_ThatReturnAwaitableTypes_ShouldHaveAsyncSuffix()
{
    var rule = MethodMembers().That()
                       .AreDeclaredIn(Repositories)
                       .Should()
                       .HaveReturnType(
                            Types().That()
                            .Are(Pocos)
                       );

    rule.Check(Architecture);
}

Imagining there are the following methods

public Task<NoPocoObject> GetAsync(){}

--> ❌ Violation should be found

public Task<PocoObject> GetAsync(){}

--> ✅ no Violation should be found (/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question.
Projects
None yet
Development

No branches or pull requests

3 participants