Repository navigation
Conversation
Some other parts of Moq (e.g. the `ReturnBase` behavior or default value providers) already produce awaitables that needn't be lifted.
* `IAwaitableHandler` needs to become public so that it can be implemented for user-defined awaitable types. * `Await` methods for user-defined awaitable types may be defined any- where. They are required to be static and have exactly one parameter. That parameter gets used to look up a suitable `IAwaitableHandler`. * Handlers are registered with `AwaitableHandler.Register`. (For this reason `AwaitableHandler` is also made public.)
... by combining the existing `IAwaitableHandler` & `AwaitableHandler`.
This makes both default value providers and "inner mock" discovery work seamlessly for all known awaitable types. Also reduces code duplication a little.
stakx
commented
Dec 30, 2020
Comment on lines
+281
to
+285
| case ExpressionType.Extension when (e is AwaitExpression awaitExpression): | ||
| { | ||
| Split(awaitExpression.Operand, out r, out p); | ||
| p.AwaitableHandler = awaitExpression.AwaitableHandler; | ||
| return; |
Contributor
Author
There was a problem hiding this comment.
An AwaitExpression appears to get thrown away here. This might cause the "await" to disappear from the resulting setup's Expression.
Comment on lines
+601
to
+603
| // NOTE: Depending on the exact scenario, the `Await` method name may get converted to | ||
| // a faux `await` keyword (e.g. with `SetupSet`). Right now, we don't care too much about | ||
| // that slight loss of accuracy, as long as a clue for the occurred await remains. |
Contributor
Author
There was a problem hiding this comment.
Perhaps, for overall consistency, all setup expressions should be post-processed to rewrite Await calls as (await …). That might not be worth the trouble if we later decide to go in the oppsosite direction (i.e. if we want to try and preserve the original Await method identity in expression trees reconstructed from delegates).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an improved redo of my previous PR #1008, and closes #1007.
What is this?
This PR achieves "await anything, anywhere" for Moq. It allows you to await tasks and other custom awaitable types right inside setup expressions:
If the
awaitkeyword were supported in LINQ expression trees, we wouldn't need a substitute method; but this is probably as close as we can get to C# syntax today.See #1007 for a few more examples how to use this.
Benefits over the existing async API (
.ReturnsAsync,.ThrowsAsync, etc.)Using
Await, you can chain across async calls inside a single setup expressions; something that the existing APIs don't cover at all:It works in places where there currently aren't any dedicated async methods, e.g. in
Mock.Of:The existing dedicated async methods aren't as feature-complete as the main setup API, and bringing them up to feature parity is a constant effort. Because
Awaitis designed in a way that does not add any new setup methods, any feature added to the main API should automatically work for async methods, too.Writing a custom await operator:
task.Result()instead of.Await(task)Would you rather have your setup look like this?
This is fairly easy to do. Simply add this to your code:
Note that custom await-like operators need to return an actual value in order to work properly with
.SetupSet,.SetupAdd, and.SetupRemove. In all other cases, the representation of your await method in the setup expression will only get inspected, but never actually called. (And even when it does get called by.SetupSetand friends, the passed-in tasks / awaitable should already have completed successfully, so.Resultwill be non-blocking.)Custom awaitable types
If you have any custom awaitable type that you want to use with Moq, you'll have to provide a little guidance and register a dedicated handler. Say this is your custom awaitable:
You will need to instruct Moq...
Cup<>,Cup<>, andTContentsvalue out of an existingCup<>.This is done by registering an implementation of
Moq.Async.AwaitableHandler:A third method
TryGetResultfor extracting theTContentsvalue is provided by the base class. (It will attempt to fetch the result via the awaiter.) Consider providing your own implementation for better performance.Once you register your handler with the Moq infrastructure, it will recognize your type and be able to provide default values etc.:
If you want to await your cups in setup expressions, you'll need a custom
Awaitmethod for static type safety:(As mentioned above, the return value is only a dummy value to help along
.SetupSetand friends.)You're done: