I am getting a NotSupportedException error message on my Unit Test using Moq.
I have the following test which mocks the result.
public class Person
{
public string Id { get; set; }
}
[Test]
public void List_Should_List_All_People()
{
//Arrange
const long total = 3;
var list = new List<Person>();
var queryResponse = new Mock<Task<ISearchResponse<Person>>>();
queryResponse.Setup(x => x.Result.Total).Returns(total);
for (var i = 0; i < total; i++)
{
list.Add(new Person { Id = Guid.NewGuid().ToString() });
}
queryResponse.Setup(x => x.Result.Documents).Returns(list);
Thread.Sleep(2000);
_elasticClient.Setup(x => x.SearchAsync(It.IsAny<Func<SearchDescriptor<Person>, ISearchRequest>>())).Returns(queryResponse.Object);
// Act
var response = _repository.List<Person>();
//Assert
response.Count().Should().Be(3);
}
queryResponse.Setup(x => x.Result.Total).Returns(total); <-- an exception is thrown
Invalid setup on a non-virtual (overridable in VB) member: mock => mock.Result
It works synchronously without any problems.
Any suggestions how to get around this exception?
How can I set it as virtual?