diff --git a/src/NSubstitute/Core/WhenCalled.cs b/src/NSubstitute/Core/WhenCalled.cs index 6ba33312..6effb4c8 100644 --- a/src/NSubstitute/Core/WhenCalled.cs +++ b/src/NSubstitute/Core/WhenCalled.cs @@ -70,4 +70,25 @@ public void Throw(Exception exception) => /// public void Throw(Func createException) => Do(ci => throw createException(ci)); + + /// + /// Throws the specified exception when called. + /// + /// Prefer for readability. + public void Throws(Exception exception) => + Throw(exception); + + /// + /// Throws an exception of the given type when called. + /// + /// Prefer for readability. + public TException Throws() where TException : Exception, new() + => Throw(); + + /// + /// Throws an exception generated by the specified function when called. + /// + /// Prefer for readability. + public void Throws(Func createException) => + Throw(createException); } \ No newline at end of file diff --git a/tests/NSubstitute.Acceptance.Specs/WhenCalledDo.cs b/tests/NSubstitute.Acceptance.Specs/WhenCalledDo.cs index 8b001a0e..60e19b1d 100644 --- a/tests/NSubstitute.Acceptance.Specs/WhenCalledDo.cs +++ b/tests/NSubstitute.Acceptance.Specs/WhenCalledDo.cs @@ -80,6 +80,19 @@ public void Throw_exception_when_Throw_with_generic_exception() Assert.That(called, Is.EqualTo(1)); } + [Test] + public void Throw_exception_when_Throws_with_generic_exception() + { + int called = 0; + _something.When(x => x.Echo(Arg.Any())).Do(x => called++); + var expectedException = _something.When(x => x.Echo(Arg.Any())).Throws(); + + Assert.That(called, Is.EqualTo(0), "Should not have been called yet"); + ArgumentException actualException = Assert.Throws(() => _something.Echo(1234)); + Assert.That(actualException, Is.EqualTo(expectedException)); + Assert.That(called, Is.EqualTo(1)); + } + [Test] public void Throw_exception_when_Throw_with_specific_exception() { @@ -94,6 +107,20 @@ public void Throw_exception_when_Throw_with_specific_exception() Assert.That(called, Is.EqualTo(1)); } + [Test] + public void Throw_exception_when_Throws_with_specific_exception() + { + var exception = new IndexOutOfRangeException("Test"); + int called = 0; + _something.When(x => x.Echo(Arg.Any())).Do(x => called++); + _something.When(x => x.Echo(Arg.Any())).Throws(exception); + + Assert.That(called, Is.EqualTo(0), "Should not have been called yet"); + IndexOutOfRangeException thrownException = Assert.Throws(() => _something.Echo(1234)); + Assert.That(thrownException, Is.EqualTo(exception)); + Assert.That(called, Is.EqualTo(1)); + } + [Test] public void Throw_exception_when_Throw_with_exception_generator() { @@ -108,6 +135,20 @@ public void Throw_exception_when_Throw_with_exception_generator() Assert.That(called, Is.EqualTo(1)); } + [Test] + public void Throw_exception_when_Throws_with_exception_generator() + { + Func createException = ci => new ArgumentException("Argument: " + ci.Args()[0]); + int called = 0; + _something.When(x => x.Echo(Arg.Any())).Do(x => called++); + _something.When(x => x.Echo(Arg.Any())).Throws(createException); + + Assert.That(called, Is.EqualTo(0), "Should not have been called yet"); + ArgumentException thrownException = Assert.Throws(() => _something.Echo(1234)); + Assert.That(thrownException.Message, Is.EqualTo("Argument: 1234")); + Assert.That(called, Is.EqualTo(1)); + } + [Test] public async Task Can_configure_async_methods_nicely() {