Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When(...).Throws gives a CouldNotSetReturnDueToNoLastCallException (#803) #818

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/NSubstitute/Core/WhenCalled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,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>
/// Prefer <see cref="Throw(System.Exception)" /> for readability.
public void Throws(Exception exception) =>
Throw(exception);

/// <summary>
/// Throws an exception of the given type when called.
/// </summary>
/// Prefer <see cref="Throw{TException}" /> for readability.
public TException Throws<TException>() where TException : Exception, new()
=> Throw<TException>();

/// <summary>
/// Throws an exception generated by the specified function when called.
/// </summary>
/// Prefer <see cref="Throw(System.Func{CallInfo, System.Exception})" /> for readability.
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
Loading