C# 模拟采用默认为null的可选参数的方法
我正在编写一个针对一个函数的单元测试,该函数通过NEST命中Elasticsearch。我的单元测试设置如下所示:C# 模拟采用默认为null的可选参数的方法,c#,.net,
elasticsearch,moq,nest,C#,.net,
elasticsearch,Moq,Nest,我正在编写一个针对一个函数的单元测试,该函数通过NEST命中Elasticsearch。我的单元测试设置如下所示: var mockResponse = new Mock<IBulkResponse>(); var mockClient = new Mock<IElasticClient>(); mockClient.Setup(x => x.IndexManyAsync<Store>(It.IsAny<IEnumerable<Store&g
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));
我不清楚这里发生了什么。为什么我无法模拟这个采用可选参数的方法?看来,indexmanyanc的这个特殊重载:
公共静态任务IndexManySync(此IElasticClient客户端,
IEnumerable对象,
字符串索引=null,
字符串类型=null),其中T:class
{
//
}
公共静态任务IndexManySync(此IElasticClient客户端,
IEnumerable对象,
字符串索引=null,
字符串类型=null),其中T:class
{
//
}
.IndexManySync是一种扩展方法吗?IndexManySync是一种扩展方法吗?
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>())
public static Task<IBulkResponse> IndexManyAsync<T>(this IElasticClient client,
IEnumerable<T> objects,
string index = null,
string type = null) where T : class
{
// <snip>
}