Skip to content
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
16 changes: 13 additions & 3 deletions Source/Mockolate/MockRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Text;
using Mockolate.Exceptions;
using Mockolate.Interactions;
Expand Down Expand Up @@ -78,11 +79,13 @@ public MethodSetupResult<TResult> InvokeMethod<TResult>(string methodName,
$"The method '{methodName}({string.Join(", ", parameters.Select(x => x.Value?.GetType().FormatType() ?? "<null>"))})' was invoked without prior setup.");
}

return new MethodSetupResult<TResult>(null, Behavior, defaultValue(parameters.Select(x => x.Value).ToArray()));
return new MethodSetupResult<TResult>(null, Behavior,
defaultValue(parameters.Select(x => x.Value).ToArray()));
}

return new MethodSetupResult<TResult>(matchingSetup, Behavior,
matchingSetup.Invoke(methodInvocation, Behavior, () => defaultValue(parameters.Select(x => x.Value).ToArray())));
matchingSetup.Invoke(methodInvocation, Behavior,
() => defaultValue(parameters.Select(x => x.Value).ToArray())));
}

/// <summary>
Expand Down Expand Up @@ -176,7 +179,14 @@ public void Raise(string eventName, params object?[] parameters)
{
foreach ((object? target, MethodInfo method) in GetEventHandlers(eventName))
{
method.Invoke(target, parameters);
try
{
method.Invoke(target, parameters);
}
catch (TargetInvocationException ex) when (ex.InnerException is not null)
{
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
}
Comment thread
vbreuss marked this conversation as resolved.
}
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/Mockolate.Tests/MockEvents/RaiseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ public async Task WhenSubscribedToOtherEvent_ShouldNotTrigger()
await That(callCount).IsEqualTo(0);
}

[Fact]
public async Task WhenSubscriptionThrows_ShouldNotWrapException()
{
IMyEventService mock = Mock.Create<IMyEventService, IMyEventServiceBase1>();

mock.SomeEvent += SubscriptionThrowingException;

void Act()
{
mock.RaiseOnMock.SomeEvent(this, "event data");
}

await That(Act).Throws<MockException>().WithMessage("Subscription exception");

void SubscriptionThrowingException(object? sender, string e)
{
throw new MockException("Subscription exception");
}
}

[Fact]
public async Task WhenUnsubscribedFromOtherEvent_ShouldNotAffectOtherSubscriptions()
{
Expand Down
Loading