【问题标题】:Mock elastic client call模拟弹性客户端调用
【发布时间】:2017-02-26 20:50:04
【问题描述】:

我正在尝试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>()
);

【问题讨论】:

    标签: c# moq nest


    【解决方案1】:

    我认为你的第一个验证条件有问题:

    docPath => docPath == DocumentPath<Document>.Id(document)
    

    正如您在NEST source code 中看到的,DocumentPath 类不会重载 == 运算符 - 它会检查引用相等性。 DocumentPath.Id(document) 创建新对象,因此条件始终为假。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-07
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多