Describe the bug
I am using the Fluent Assertions for clear assertions but it doesn't affect this bug.
I have interface with the following method
void ReduceProductAvailability(int productId, int orderedQuantity);
When I create the substitute which throws exception for this
var service = Substitute.For<IProductService>();
service
.When(e => e.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>()))
.Do(x => throw new NotFoundException("XYZ"));
It not throws when in execution I use
service.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>())
but it works if I use simple parameters
service.ReduceProductAvailability(0, 0)
To Reproduce
Compare two of the following tests
This works
// Arrange
var service = Substitute.For<IProductService>();
service
.When(e => e.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>()))
.Do(x => throw new NotFoundException("XYZ"));
// Act
Action result = () => service.ReduceProductAvailability(0, 0);
// Assert
result.Should().Throw<NotFoundException>();
This not
// Arrange
var service = Substitute.For<IProductService>();
service
.When(e => e.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>()))
.Do(x => throw new NotFoundException("XYZ"));
// Act
Action result = () => service.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>());
// Assert
result.Should().Throw<NotFoundException>();
Expected behaviour
It should throws the exception in both cases
Environment:
The second examples do not work because you are using argument matcher in a real call.
See documentation . In general Arg matchers should only be used for mock setup and checking received calls. See also feature request https://github.com/nsubstitute/NSubstitute.Analyzers/issues/35#issue-355796854 for NSubstitute.Analyzers with quick description about the proper usage
Hi,
You're right.
I checked the documentation, but I didn't find this.
Thank you, I will close this issue.
Most helpful comment
Hi,
You're right.
I checked the documentation, but I didn't find this.
Thank you, I will close this issue.