C# 尝试模拟Azure搜索模型上的字段数时不支持的表达式

C# 尝试模拟Azure搜索模型上的字段数时不支持的表达式,c#,nunit,moq,azure-cognitive-search,C#,Nunit,Moq,Azure Cognitive Search,我尝试使用以下方法进行单元测试: public bool CompareIndexEquality(Index resultBody, Type indexType) { var properties = indexType.GetProperties(BindingFlags.Instance | BindingFlags.Public); if (properties.Count() == resultBody.Fields.

我尝试使用以下方法进行单元测试:

 public bool CompareIndexEquality(Index resultBody, Type indexType)
        {
            var properties = indexType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            if (properties.Count() == resultBody.Fields.Count)
            {
                HavePropertyAttributesChanged(properties);
            }
            return false;
        }
我的尝试是:

[Test]
        public void CompareIndexEquality_AnyCase_ReturnsTrueIfAttributesMatch()
        {
            var compareSearchIndexService = new CompareSearchIndexService();
            var indexTypeMock =  new Mock<Type>();
            var resultBodyMock = new Mock<Microsoft.Azure.Search.Models.Index>();
            var lookUpIndexModel = new LookUpIndexModel();
            PropertyInfo[] propertyInfo = lookUpIndexModel.GetType().GetProperties();


            indexTypeMock.Setup(r => r.GetProperties(BindingFlags.Instance | BindingFlags.Instance)).Returns(propertyInfo);
            resultBodyMock.Setup(r => r.Fields).Returns(new List<Field>());
            var result = compareSearchIndexService.CompareIndexEquality(resultBodyMock.Object, indexTypeMock.Object);
            Assert.IsFalse(result);
        }
[测试]
public void CompareIndexEquality\u AnyCase\u ReturnsTrueIfAttributesMatch()
{
var compareSearchIndexService=新的compareSearchIndexService();
var indexTypeMock=new Mock();
var resultBodyMock=new Mock();
var lookUpIndexModel=新的lookUpIndexModel();
PropertyInfo[]PropertyInfo=lookUpIndexModel.GetType().GetProperties();
Setup(r=>r.GetProperties(BindingFlags.Instance | BindingFlags.Instance)).Returns(propertyInfo);
resultBodyMock.Setup(r=>r.Fields).Returns(newlist());
var result=compareSearchIndexService.CompareIndexEquality(resultBodyMock.Object,indexTypeMock.Object);
Assert.IsFalse(结果);
}
我收到错误消息: $exception{“不受支持的表达式:r=>r.Fields\n不可重写的成员(此处为Index.get_字段)不能在安装/验证表达式中使用。“}System.NotSupportedException

有人知道我如何模拟Microsoft.Azure.Search.Models.Index上的字段吗


谢谢

Moq创建了模拟类型的实现。如果类型是一个类,它将创建一个继承类,并且该继承类的成员将调用基类。但为了做到这一点,它必须凌驾于成员之上。如果一个类有不能被重写的成员(它们不是虚拟的、抽象的),那么Moq不能重写它们来添加它自己的行为

我们如何决定是否嘲笑某件事

一般来说,如果我们不想在测试中包含具体的运行时实现,我们会模拟一些东西。我们想测试一个类,而不是同时测试两个类


但在本例中,
CompareIndexEquality
只是一个判断数据的类嘲笑它真的没有意义。使用真实的东西同样容易。

SetupGet有效吗?@madreflection不幸的是,无效:相同的错误消息索引是一个纯数据类——为什么需要模拟?另外,请查看新的.NET SDK:Microsoft.Azure.Search包不再维护。