Skip to content

Commit

Permalink
When(...).Throws gives a CouldNotSetReturnDueToNoLastCallException (#803
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Mihnea Rădulescu authored and dtchepak committed Jun 2, 2024
1 parent b7ff02c commit 0c0d798
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/NSubstitute/Core/WhenCalled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace NSubstitute.Core;

public class WhenCalled<T>(ISubstitutionContext context, T substitute, Action<T> 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;
Expand Down Expand Up @@ -70,4 +73,25 @@ public void Throw(Exception exception) =>
/// </summary>
public void Throw(Func<CallInfo, Exception> createException) =>
Do(ci => throw createException(ci));

/// <summary>
/// Throws the specified exception when called.
/// </summary>
[Obsolete(RecommendedThrowMethodToUseMessage)]
public void Throws(Exception exception) =>
Throw(exception);

/// <summary>
/// Throws an exception of the given type when called.
/// </summary>
[Obsolete(RecommendedThrowMethodToUseMessage)]
public TException Throws<TException>() where TException : Exception, new()
=> Throw<TException>();

/// <summary>
/// Throws an exception generated by the specified function when called.
/// </summary>
[Obsolete(RecommendedThrowMethodToUseMessage)]
public void Throws(Func<CallInfo, Exception> createException) =>
Throw(createException);
}
41 changes: 41 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/WhenCalledDo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>())).Do(x => called++);
var expectedException = _something.When(x => x.Echo(Arg.Any<int>())).Throws<ArgumentException>();

Assert.That(called, Is.EqualTo(0), "Should not have been called yet");
ArgumentException actualException = Assert.Throws<ArgumentException>(() => _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()
{
Expand All @@ -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<int>())).Do(x => called++);
_something.When(x => x.Echo(Arg.Any<int>())).Throws(exception);

Assert.That(called, Is.EqualTo(0), "Should not have been called yet");
IndexOutOfRangeException thrownException = Assert.Throws<IndexOutOfRangeException>(() => _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()
{
Expand All @@ -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<CallInfo, Exception> createException = ci => new ArgumentException("Argument: " + ci.Args()[0]);
int called = 0;
_something.When(x => x.Echo(Arg.Any<int>())).Do(x => called++);
_something.When(x => x.Echo(Arg.Any<int>())).Throws(createException);

Assert.That(called, Is.EqualTo(0), "Should not have been called yet");
ArgumentException thrownException = Assert.Throws<ArgumentException>(() => _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()
{
Expand Down

0 comments on commit 0c0d798

Please sign in to comment.