Skip to content

Commit

Permalink
Returns(InvocationFunc) shouldn't throw TargetInvocationException
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Feb 2, 2021
1 parent f36d3e8 commit 0ddfdb8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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

0 comments on commit 0ddfdb8

Please sign in to comment.