devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.94k stars 802 forks source link

Allow passing method parameters to Throws/ThrowsAsync #1296

Open jdx-john opened 2 years ago

jdx-john commented 2 years ago

In much the same way you can pass (bind?) parameters to Returns:

mock.Setup(x => x.GetIdsAsync(It.IsAny<int>()))
    .ReturnsAsync((int n) => new List<int>(n));

It would be great if you could do the same for exception testing where you want to test the actual details of the exception:

mock.Setup(x => x.GetIdsAsync(It.IsAny<int>()))
    .ThrowsAsync((int n) => new InvalidOperationException($"{n} is not valid"));

I was suggested to use something like the following as a workaround, but it would be a useful enhancement I think.

int n = 0;
mock
    .Setup(_ => _.GetIdsAsync(It.IsAny<int>()))
    .Callback((int arg) => n = arg)
    .ThrowsAsync(new InvalidOperationException($"{n} is not valid"));

Back this issue Back this issue

github-actions[bot] commented 2 months ago

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.

RSAU-se commented 1 month ago

This enhancement would be great. Really useful for exceptions that have custom properties.