I am trying to unit test code that uses SearchClient.SearchAsync() method. I am using AutoFixture.AutoMoq nuget package.
Here is what I tried:
mockSearchClient.Setup(msc => msc.SearchAsync<MyModel>(
It.IsAny<string>(),
It.IsAny<SearchOptions>(),
It.IsAny<CancellationToken>()
)).Returns(Task.FromResult(<<PROBLEM HERE>>));
The problem lies in the parameter .Returns(Task.FromResult(<<PROBLEM HERE>>)) part. It expects a concrete object that is returned from the .SearchAsync() method. According to docs and autocomplete, the method returns Azure.Response which is an abstract class. So, I cannot new it up. In actuality, the method returns a descendant class Azure.ValueResponse, which isn't abstract, but is internal to Azure SDK, so also impossible to new up.
So how does one mock the SearchClient.SearchAsync?
P.S. Using Azure.Search.Documents, v11.1.1.0
See https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#mocking for information. Basically, you can use
Response.FromValuealong with theSearchModelFactory(a pattern we follow with all our Azure.* client SDKs for models that can't be fully mocked with a constructor and/or settable properties) to create a mock like so (using Moq, since I'm unfamiliar with AutoMoq, but should be similar):