I'm using Moq to mock a SingleBucket object in ElasticSearch. Basically I need to have it return 5 documents (property .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
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. You can either wrap SingleBucket
into an interface, either use another mocking framework .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.