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

Better (Async) support for method returning nongeneric Task #1328

Closed
JeppeSN opened this issue Jan 23, 2023 · 2 comments
Closed

Better (Async) support for method returning nongeneric Task #1328

JeppeSN opened this issue Jan 23, 2023 · 2 comments
Labels

Comments

@JeppeSN
Copy link

JeppeSN commented Jan 23, 2023

Seen with Moq 4.18.4.

Suppose I have this interface:

public interface I {
    string GetStuff(int input);
    void DoStuff(int input);
    Task<string> GetStuffAsync(int input);
    Task DoStuffAsync(int input);
}

So a string-returning method and a void-returning method; and async versions of both.

Then suppose I want to make setups on a strict mock like this:

        // strict mock, I only want to setup for one magical value of arg 'input'
        var mock = new Mock<I>(MockBehavior.Strict);

        mock.Setup(x => x.GetStuff(42)).Returns("stuff");  // works

        mock.Setup(x => x.DoStuff(42));  // works

        mock.Setup(x => x.GetStuffAsync(42)).ReturnsAsync("stuff");  // works


        // what about '.DoStuffAsync' then:

/* 1 */ mock.Setup(x => x.DoStuffAsync(42));  // run-time error when 'mock.Object' is used: I.DoStuffAsync(42) invocation failed with mock behavior Strict. ⏎ Invocation needs to return a value and therefore must have a corresponding setup that provides it.

/* 2 */ mock.Setup(x => x.DoStuffAsync(42)).ReturnsAsync();  // compile-time error (we do not have this method/overload)

/* 3 */ mock.Setup(x => x.DoStuffAsync(42)).Returns(Task.CompletedTask);  // OK, but shorter syntax desired by me

Then for DoStuffAsync, neither attempt 1 nor attempt 2 works. But I believe it would be natural to have a short syntax here, just like the short syntax seen in the Setup for the void-returning DoStuff above.

Also, with Task<string>, we offer stuff like:

mock.Setup(x => x.GetStuffAsync(42)).ReturnsAsync("stuff", delay: TimeSpan.FromMilliseconds(123.4));

so would it not be expected that you could also give a delay for DoStuffAsync:

/* 2b */ mock.Setup(x => x.DoStuffAsync(42)).ReturnsAsync(delay: TimeSpan.FromMilliseconds(123.4));  // compile-time error (we do not have this method/overload)

Back this issue
Back this issue

Copy link

Due to lack of recent activity, this issue has been labeled as 'stale'.
It will be closed if no further activity occurs within 30 more days.
Any new comment will remove the label.

@github-actions github-actions bot added the stale label Aug 24, 2024
@github-actions github-actions bot removed the stale label Sep 3, 2024
@kzu kzu added the stale label Sep 3, 2024
Copy link

github-actions bot commented Oct 4, 2024

This issue will now be closed since it has been labeled 'stale' without activity for 30 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants