我想模拟一个返回Dictionary的Linq表达式
Dictionary<string, string> properties = new Dictionary<string, string>()
{
{ "Service", "serviceTest" }
}equipment.SetProperties(dmsElement.Properties.ToDictionary(x => x.Definition.Name, x => x.Value));这是单元测试
fakeEquipment.Setup(e => e.Properties.ToDictionary(It.IsAny<Func<IDmsElementProperty, string>>(), It.IsAny<Func<IDmsElementProperty, string>>())).Returns(properties);我得到了以下错误:
System.NotSupportedException:‘不支持的表达式:. => ....ToDictionary(It.IsAny>(),It.IsAny>())
如何设置ToDictionary方法以检索预期值?
发布于 2022-02-11 11:57:40
您不能模拟ToDictionary方法,因为它是在System.Enumerable类型中找到的扩展方法。无论如何,该方法都将枚举您的.Properties属性。但是,您可以并且应该直接模拟e.Properties。
您可以返回IPropertyCollection接口的实现,也可以编写一个非常简单的适配器,公开底层集合(无论是字典还是列表)。如果您可以访问现有的类(例如,ListPropertyCollection<T>),则只需实例化它。
https://stackoverflow.com/questions/71079359
复制相似问题