I have a method that inserts documents into Azure cognitive search but I am struggling to unit-test the method. My method looks like below:
public async Task AddDocumentIntoIndexAsync(Document[] Documents, string IndexName)
{
IndexDocumentsBatch<CreClaim> batch = IndexDocumentsBatch.MergeOrUpload(Documents);
try
{
IndexDocumentsResult result = await _searchClient.IndexDocumentsAsync(batch);
}
catch (Exception ex)
{
}
}
}
I am using dependency injection to inject _searchClient which is an instance of type Azure.Search.Documents.SearchClient.
In my unit test project, I am mocking my SearchClient as below:
_searchClient = new Mock<SearchClient>(new Uri(endpoint),indexName, new AzureKeyCredential(key));
The problem happens when I try to setup IndexDocumentsAsync.
var indexresult = SearchModelFactory.IndexDocumentsResult;
_searchClient.Setup(x => x.IndexDocumentsAsync(batch, options, new CancellationToken())).Returns(indexresult);
When I run this code, I get a runt time error:
: 'Invalid callback. Setup on method with 3 parameter(s) cannot invoke callback with different number of parameters (1).'
I thought may be its because the last 2 params of the method IndexDocumentsAsync are optional so I tried with
_searchClient.Setup(x => x.IndexDocumentsAsync(batch)).Returns(indexresult);
With this I get a compilation issue:
Can anyone help please?