From 0c0d798acb01ab5d6d4da28e4f0db0b749637279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mihnea=20R=C4=83dulescu?= <> Date: Mon, 27 May 2024 21:02:55 +0300 Subject: [PATCH] When(...).Throws gives a CouldNotSetReturnDueToNoLastCallException (#803) --- src/NSubstitute/Core/WhenCalled.cs | 24 +++++++++++ .../WhenCalledDo.cs | 41 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/NSubstitute/Core/WhenCalled.cs b/src/NSubstitute/Core/WhenCalled.cs index 6ba33312..b1910cd7 100644 --- a/src/NSubstitute/Core/WhenCalled.cs +++ b/src/NSubstitute/Core/WhenCalled.cs @@ -7,6 +7,9 @@ namespace NSubstitute.Core; public class WhenCalled(ISubstitutionContext context, T substitute, Action call, MatchArgs matchArgs) { + private const string RecommendedThrowMethodToUseMessage = + "The recommended method to use is Throw()."; + private readonly ICallRouter _callRouter = context.GetCallRouterFor(substitute!); private readonly IThreadLocalContext _threadContext = context.ThreadContext; private readonly IRouteFactory _routeFactory = context.RouteFactory; @@ -70,4 +73,25 @@ public void Throw(Exception exception) => /// public void Throw(Func createException) => Do(ci => throw createException(ci)); + + /// + /// Throws the specified exception when called. + /// + [Obsolete(RecommendedThrowMethodToUseMessage)] + public void Throws(Exception exception) => + Throw(exception); + + /// + /// Throws an exception of the given type when called. + /// + [Obsolete(RecommendedThrowMethodToUseMessage)] + public TException Throws() where TException : Exception, new() + => Throw(); + + /// + /// Throws an exception generated by the specified function when called. + /// + [Obsolete(RecommendedThrowMethodToUseMessage)] + 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() {