c# - 使用 Moq 在 ElasticSearch 中模拟单个 Bucket 对象

标签 c# unit-testing elasticsearch mocking moq

我正在使用 Moq 在 ElasticSearch 中模拟 SingleBucket 对象。基本上我需要让它返回 5 个文档(属性 .DocCount )

var sb = new Mock<SingleBucket>();
sb.SetupGet(x => x.DocCount).Returns(5);

我试过了,我得到了一个异常(exception):
{System.NotSupportedException: Invalid setup on a non-virtual
(overridable in VB) member: x => x.DocCount

这段代码有什么问题?
似乎可以使用 SetupGet 模拟属性

我知道我应该模拟接口(interface)或虚拟方法。有没有机会模拟该对象属性?

最佳答案

正如您已经指定的那样,Moq不能模拟非虚拟方法。您可以包装 SingleBucket进入界面,使用another mocking framework .

关于c# - 使用 Moq 在 ElasticSearch 中模拟单个 Bucket 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016290/

相关文章:

python - Django 测试 : how to specify the apps whose databases I want to use?

elasticsearch - Elasticsearch:转换量翻倍至geo_point。无效的geohash字符错误

c# - 串口传输时数据损坏

unit-testing - 角度 : ng test runs but doesnt execute test cases

C# 自定义属性未应用

c# - 如何在 Unity Test Runner 中测试 Unity 协程

elasticsearch - 如何在Elasticsearch中按文档数组中的对象的多个字段查询?

node.js - ElasticSearch:如何使用 ElasticSearch API 仅返回特定字段?

c# - 当我使用多个项目替代一个项目中多个目录的 C# 时,是否存在性能损失

C# 类和方法