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

Ensure Returns(InvocationFunc) doesn't throw TargetInvocationException #1141

Merged
merged 1 commit into from
Feb 2, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
* `AmbiguousMatchException` raised when interface has property indexer besides property in VB. (@mujdatdinc, #1129)
* Interface default methods are ignored (@hahn-kev, #972)
* Callback validation too strict when setting up a task's `.Result` property (@stakx, #1132)
* `setup.Returns(InvocationFunc)` wraps thrown exceptions in `TargetInvocationException` (@stakx, #1141)


## 4.16.0 (2021-01-16)
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void SetReturnComputedValueBehavior(Delegate valueFactory)
}
else if (IsInvocationFunc(valueFactory))
{
this.returnOrThrow = new ReturnComputedValue(invocation => valueFactory.DynamicInvoke(invocation));
this.returnOrThrow = new ReturnComputedValue(invocation => valueFactory.InvokePreserveStack(new object[] { invocation }));
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Moq.Tests/CallbacksFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,19 @@ public void CallbackWithMultipleArgumentIndexerSetterWithoutAny()
Assert.Equal(2, result);
}

[Fact]
public void Type_of_exception_thrown_from_InvocationFunc_callback_should_be_preserved()
{
var mock = new Mock<IFoo>();
mock.Setup(m => m.Submit("good", "bad")).Returns(new InvocationFunc(invocation =>
{
throw new Exception("very bad"); // this used to be erroneously wrapped as a `TargetInvocationException`
}));

var ex = Assert.Throws<Exception>(() => mock.Object.Submit("good", "bad"));
Assert.Equal("very bad", ex.Message);
}

public interface IInterface
{
void Method(Derived b);
Expand Down