moq icon indicating copy to clipboard operation
moq copied to clipboard

Explicit support for matching cancellation tokens with It class. (It.IsCancellationToken())

Open mjamro opened this issue 1 year ago • 1 comments

Category

  • [x] Feature request

Describe the feature

When mocking async methods that require a cancellation token you can use It.IsAny<CancellationToken>() syntax like this:

myMock.Setup(m => m.DoSomethingAsync(It.IsAny<CancellationToken>()));

I propose adding a new method to It class for matching cancellation tokens, that does exactly the same:

myMock.Setup(m => m.DoSomethingAsync(It.IsCancellationToken()));

Rationale:

  • It's slightly more readable and less verbose than It.IsAny<> syntax.. Readable code is good 👍
  • Async methods are extremely common and mocking them is common as well.
  • It's purely syntactic sugar. It breaks nothing.
  • Code completion will be easier. Right now you need to do it in two steps (IsAny + CancellationToken in generic params).
  • It class is static, so you cannot add an extension method with similar behaviour on your own.

Back this issue Back this issue

mjamro avatar May 05 '24 17:05 mjamro

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[bot] avatar May 07 '25 01:05 github-actions[bot]