Mocking IEventGridClient using the Moq library

Ershad Nozari 421 Reputation points
2021-01-22T13:03:07.3+00:00

I'm trying to mock IEventGridClient from the Microsoft.Azure.EventGrid NuGet package with the Moq library for unit testing purposes.

Mock<IEventGridClient> client = new();

client.Setup(x 
 => x.PublishEventsAsync(It.IsAny<string>(),
 It.IsAny<List<EventGridEvent>>(), 
 default))
 .Returns(Task.CompletedTask);

However, upon calling Setup I'm getting below exception :

    System.NotSupportedException : Unsupported expression: x => x.PublishEventsAsync(It.IsAny<string>(), It.IsAny<List<EventGridEvent>>(), CancellationToken)
    Extension methods (here: EventGridClientExtensions.PublishEventsAsync) may not be used in setup / verification expressions.
  Stack Trace: 
    Guard.IsOverridable(MethodInfo method, Expression expression) line 87
    InvocationShape.ctor(LambdaExpression expression, MethodInfo method, IReadOnlyList`1 arguments, Boolean exactGenericTypeArguments, Boolean skipMatcherInitialization, Boolean allowNonOverridable) line 84
    ExpressionExtensions.<Split>g__Split|5_0(Expression e, Expression& r, InvocationShape& p, Boolean assignment, Boolean allowNonOverridableLastProperty) line 234
    ExpressionExtensions.Split(LambdaExpression expression, Boolean allowNonOverridableLastProperty) line 149
    Mock.SetupRecursive[TSetup](Mock mock, LambdaExpression expression, Func`4 setupLast, Boolean allowNonOverridableLastProperty) line 606
    Mock.Setup(Mock mock, LambdaExpression expression, Condition condition) line 510
    Mock`1.Setup[TResult](Expression`1 expression) line 468
    UnitTests.ShouldStopAppOnSqlExceptionWhenDeltaEventIsNull() line 25
    --- End of stack trace from previous location ---

What is the recommended approach to unit testing using the Microsoft.Azure.EventGrid SDK. For the moment, I have written a wrapper class around EventGridClient, however I consider this unnecessary as the class already implements an interface, IEventGridClient.

Appreciate any guidance.

Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
376 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,842 questions
0 comments No comments
{count} votes

Accepted answer
  1. JayaC-MSFT 5,531 Reputation points
    2021-01-28T17:31:23.617+00:00

    Hello @Ershad Nozari I am afraid, you can not mock that directly as PublishEventsAsync is an extension method of IEventGridClient, but you should be able to mock the method PublishEventsWithHttpMessages of IEventGridClient.
    Please refer to: https://video2.skills-academy.com/dotnet/api/microsoft.azure.eventgrid.ieventgridclient.publisheventswithhttpmessagesasync
    In short, if you mock PublishEventsWithHttpMessagesAsync, the extension method PublishEventsAsync will be taken care since internally PublishEventsAsync calls PublishEventsWithHttpMessagesAsync internally. Your actual code still can use PublishEventsAsync, it's just mocking trick in your unit test.

    client.Setup(x  
      => x.PublishEventsWithHttpMessagesAsync(It.IsAny<string>(),  
      It.IsAny<IList<EventGridEvent>>(),  
      It.IsAny<Dictionary<List<string>>>(),  
      It.IsAny<CancellationToken >()))  
      .ReturnsAsync(new AzureOperationResponse());  
    

    Please let me know if this helps.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,221 Reputation points
    2021-01-22T23:45:16.21+00:00

    IMHO, this event grid is something that you should not be unit testing. You should be unit testing the code the event grid is acting upon or using not the event grid itself.

    Not everything is unit testable.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.