尝试模拟 Azure 搜索模型上的字段数时不支持的表达式
Unsupported expression when trying to mock the number of Fields on Azure Search Model
我正在尝试对以下方法进行单元测试:
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);
}
我收到错误:
$exception {“不支持的表达式:r => r.Fields\nNon-overridable 成员(此处:Index.get_Fields)不能用于设置/验证表达式。”} System.NotSupportedException
有谁知道如何模拟 Microsoft.Azure.Search.Models.Index
上的字段?
谢谢
Moq 创建模拟类型的实现。如果类型是 class,它会创建一个继承的 class,并且继承的 class 的成员调用基础 class。但为了做到这一点,它必须覆盖成员。如果 class 具有无法覆盖的成员(它们不是虚拟的、抽象的),则 Moq 无法覆盖它们以添加自己的行为。
How do we determine whether or not to mock something?
一般来说,如果我们不想在测试中包含具体的运行时实现,我们就会模拟一些东西。我们想测试一个 class 而不是同时测试两个。
但是在这种情况下CompareIndexEquality
只是一个判断数据的class。 嘲笑它真的没有意义。使用真实的东西也一样容易。
我正在尝试对以下方法进行单元测试:
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);
}
我收到错误: $exception {“不支持的表达式:r => r.Fields\nNon-overridable 成员(此处:Index.get_Fields)不能用于设置/验证表达式。”} System.NotSupportedException
有谁知道如何模拟 Microsoft.Azure.Search.Models.Index
上的字段?
谢谢
Moq 创建模拟类型的实现。如果类型是 class,它会创建一个继承的 class,并且继承的 class 的成员调用基础 class。但为了做到这一点,它必须覆盖成员。如果 class 具有无法覆盖的成员(它们不是虚拟的、抽象的),则 Moq 无法覆盖它们以添加自己的行为。
How do we determine whether or not to mock something?
一般来说,如果我们不想在测试中包含具体的运行时实现,我们就会模拟一些东西。我们想测试一个 class 而不是同时测试两个。
但是在这种情况下CompareIndexEquality
只是一个判断数据的class。 嘲笑它真的没有意义。使用真实的东西也一样容易。