Raise Async Event causes Method Signature Error

1 Answer 8 Views
Async Mocking General Discussions Non-public Mocking
Anthony
Top achievements
Rank 1
Anthony asked on 22 Jun 2024, 01:33 AM | edited on 22 Jun 2024, 03:55 AM

For the sake of brevity, I've included a minimal example below. An async event MyEventAsync is defined in class MyClass. An event handler OnMyEventAsync is also defined.


public class MyClass
{
    // Event.
    public event Func<EventArgs, Task>? MyEventAsync;
}

// Event handler.
private Task OnMyEventAsync(EventArgs args) => Task.CompletedTask;

I then create a mock of MyClass and register the event handler OnMyEventAsync with the event MyEventAsync. Then Raise the event with the syntax given in the documentation.

// Create mock.
var mockClass = Mock.Create<MyClass>(Behavior.Strict);

// Register event handler.
mockClass.MyEventAsync += OnMyEventAsync;

// Raise event on mock.
Mock.Raise(() => mockClass.MyEventAsync += null, EventArgs.Empty);

But I receive the following error:

Telerik.JustMock.Core.MockException : Event signature System.Threading.Tasks.Task OnMyEventAsync(System.EventArgs) is incompatible with argument types (Castle.Proxies.ExternalMockMixinProxy, System.EventArgs)

I've probably missed something very obvious here. I know the above syntax should work with an event of type EventHandler<EventArgs>, but I'm not sure how to modify it to work with event of type Func<EventArgs, Task>. If anyone could clear up my confusion, I'd be most grateful. Thanks! =)

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Ivo
Telerik team
answered on 25 Jun 2024, 03:54 PM

Hello Anthony,

As you mentioned, Mock.Rise assumes that the event is EventHandler<EventArgs> compatible and tries to insert the missing argument for the sender behind the scenes. In this particular case, it becomes wrong and that is a root cause for this exception, I am afraid that it requires a change in JustMock code. Until we fix this issue, the workaround would be raising the event through reflection.

Regards,
Ivo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Anthony
Top achievements
Rank 1
commented on 28 Jun 2024, 05:33 PM

Okay, just wanted to make sure that was the case. Thanks! =)
Ivo
Telerik team
commented on 01 Jul 2024, 09:28 AM

Hey Anthony, we have already fixed the issue and will make it publicly available in 2024 the Q3 release. Thanks for your finding, we greatly appreciate it.
Tags
Async Mocking General Discussions Non-public Mocking
Asked by
Anthony
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or