The Throws
and ThrowsAsync
helpers in the NSubstitute.ExceptionExtensions
namespace can be used to throw exceptions when a member is called.
//For non-voids:
calculator.Add(-1, -1).Throws(new Exception()); // Or .Throws<Exception>()
//For voids and non-voids:
calculator
.When(x => x.Add(-2, -2))
.Throw(x => new Exception()); // Or .Throw<Exception>() - - don't use .Throw*s* in this case
//Both calls will now throw.
Assert.Throws<Exception>(() => calculator.Add(-1, -1));
Assert.Throws<Exception>(() => calculator.Add(-2, -2));
Returns
Another way is to use the underlying method, .Returns
. See also Callbacks.
//For non-voids:
calculator.Add(-1, -1).Returns(x => { throw new Exception(); });
//For voids and non-voids:
calculator
.When(x => x.Add(-2, -2))
.Do(x => { throw new Exception(); });
//Both calls will now throw.
Assert.Throws<Exception>(() => calculator.Add(-1, -1));
Assert.Throws<Exception>(() => calculator.Add(-2, -2));