简体   繁体   English

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

[英]Mocking a Single Bucket object in ElasticSearch using Moq

I'm using Moq to mock a SingleBucket object in ElasticSearch. 我正在使用Moq在ElasticSearch中模拟SingleBucket对象。 Basically I need to have it return 5 documents (property .DocCount ) 基本上我需要让它返回5个文档(属性.DocCount

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

I tried that and I get an exception: 我尝试过,但得到一个例外:

{System.NotSupportedException: Invalid setup on a non-virtual
(overridable in VB) member: x => x.DocCount

What's wrong with this code? 此代码有什么问题? Seems to be ok to mock a property using SetupGet 似乎可以使用SetupGet模拟属性

I know I should mock either the interface or a virtual method. 我知道我应该模拟接口或虚拟方法。 Is there any chance to mock that object property? 有没有机会模拟该对象属性?

As you specified already, Moq cannot mock non-virtual method. 正如您已经指定的, Moq无法模拟非虚拟方法。 You can either wrap SingleBucket into an interface, either use another mocking framework . 您可以将SingleBucket包装到一个接口中,或者使用另一个 SingleBucket 框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM