Skip to content

[Feature]: ReturnsAsync factory with actual method parameters for mocks #5361

@Kaskadee

Description

@Kaskadee

Problem Statement

The Returns method allows you to pass a factory through which you can access the arguments passed to the method. However, it seems that this feature is missing from ReturnsAsync.

Proposed Solution

A ReturnsAsync overload which allows to pass in a factory method and return a Task<T> from the method parameters:

mock
    .DoSomethingAsync(Any<Func<Task<string>>>())
    .ReturnsAsync(factory => factory.Invoke());

Alternatives Considered

It is possible to use .Returns and return Task<T>.Result:

mock
    .DoSomethingAsync(Any<Func<Task<string>>>())
    .Returns(factory => factory.Invoke().Result); // Exists but requires `Task<T>.Result`

Feature Category

Other

How important is this feature to you?

Nice to have - would improve my experience

Additional Context

No response

Contribution

  • I'm willing to submit a pull request for this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions