模拟弹性客户端调用
问题描述:
我试图对弹性客户端调用Moq
,但它不起作用。模拟弹性客户端调用
我有这个调用内部的通用repo.Update(T doc)
:
var response = await _client.UpdateAsync(DocumentPath<T>.Id(doc),
d => d
.Upsert(doc)
.Script(.....)
);
在我的测试我想验证是否UpdateAsync
与这些特定的输入调用。
我试过,没有工作的情况如下:我觉得你有与第一验证条件问题
_mock = new Mock<IElasticClient>();
Func<UpdateDescriptor<Document,Document>,
IUpdateRequest<Document,Document>> sel = d => d.Upsert(doc).Script(....);
await repo.Update(document)
_mock.Verify(c => c.UpdateAsync<Document>(
It.Is<DocumentPath<Document>(
docPath => docPath == DocumentPath<Document>.Id(document),
It.Is<Func<...>(s => s == sel),
it.IsAny<CancellationToken>()
);
答
:
docPath => docPath == DocumentPath<Document>.Id(document)
正如你可以看到NEST source code,DocumentPath
类不重载==运算符 - 它检查引用相等。 DocumentPath.Id(document)创建新对象,因此条件始终为false。
希望有所帮助。