moq icon indicating copy to clipboard operation
moq copied to clipboard

Allow passing method parameters to Throws/ThrowsAsync

Open jdx-john opened this issue 2 years ago • 1 comments

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

jdx-john avatar Nov 10 '22 10:11 jdx-john