diff --git a/build/ExtractDocs.fs b/build/ExtractDocs.fs index 466b927c..f7a56663 100644 --- a/build/ExtractDocs.fs +++ b/build/ExtractDocs.fs @@ -37,6 +37,7 @@ using System.Collections.Generic; using System.ComponentModel; using NSubstitute.Extensions; using NSubstitute.Compatibility; +using NSubstitute.ExceptionExtensions; namespace NSubstitute.Samples { public class Tests_%s { diff --git a/docs/help/_posts/2010-05-02-throwing-exceptions.markdown b/docs/help/_posts/2010-05-02-throwing-exceptions.markdown index ade4d02b..b7ef3adc 100644 --- a/docs/help/_posts/2010-05-02-throwing-exceptions.markdown +++ b/docs/help/_posts/2010-05-02-throwing-exceptions.markdown @@ -3,8 +3,6 @@ title: Throwing exceptions layout: post --- -[Callbacks](/help/callbacks) can be used to throw exceptions when a member is called. - +The `Throws` and `ThrowsAsync` helpers in the `NSubstitute.ExceptionExtensions` namespace can be used to throw exceptions when a member is called. + +```csharp +//For non-voids: +calculator.Add(-1, -1).Throws(new Exception()); // Or .Throws() + +//For voids and non-voids: +calculator + .When(x => x.Add(-2, -2)) + .Throw(x => new Exception()); // Or .Throw() - - don't use .Throw*s* in this case + +//Both calls will now throw. +Assert.Throws(() => calculator.Add(-1, -1)); +Assert.Throws(() => calculator.Add(-2, -2)); +``` + +### Returns +Another way is to use the underlying method, `.Returns`. See also [Callbacks](/help/callbacks). + ```csharp //For non-voids: calculator.Add(-1, -1).Returns(x => { throw new Exception(); }); @@ -25,4 +42,4 @@ calculator //Both calls will now throw. Assert.Throws(() => calculator.Add(-1, -1)); Assert.Throws(() => calculator.Add(-2, -2)); -``` \ No newline at end of file +```