moq
moq copied to clipboard
Allow passing method parameters to Throws/ThrowsAsync
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"));