0

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?

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159

1 Answers1

1

As you specified already, Moq cannot mock non-virtual method. You can either wrap SingleBucket into an interface, either use another mocking framework.

Community
  • 1
  • 1
Andrei Mihalciuc
  • 2,148
  • 16
  • 14