1

I am writing a unit test against a function that hits Elasticsearch through NEST. My unit test's setup looks like this:

var mockResponse = new Mock<IBulkResponse>();
var mockClient = new Mock<IElasticClient>();
mockClient.Setup(x => x.IndexManyAsync<Store>(It.IsAny<IEnumerable<Store>>(), It.IsAny<string>(), It.IsAny<string>())).Returns(Task<IBulkResponse>.Run(() => mockResponse.Object));

The IndexManyAsync function takes has a function signature of Task<IBulkResponse> IndexManyAsync<T>(IEnumerable<T> object, string index = null, string type = null).

As you can see, I tried to set up my mock IElasticClient to Mock out that method above, but I get the following exception:

An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code

Additional information: Expression references a method that does not belong to the mocked object: x => x.IndexManyAsync<Store>(It.IsAny<IEnumerable`1>(), It.IsAny<String>(), It.IsAny<String>())

It's not clear to me what is going on here. Why am I unable to mock this method that takes optional parameters?

zebra
  • 6,373
  • 20
  • 58
  • 67

1 Answers1

4

It appears that this particular overload of IndexManyAsync is an extension method:

public static Task<IBulkResponse> IndexManyAsync<T>(this IElasticClient client, 
                                                    IEnumerable<T> objects, 
                                                    string index = null, 
                                                    string type = null) where T : class
{
    // <snip>
}

You cannot use Moq to mock extension methods.

Community
  • 1
  • 1
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88