最小起订量 返回包含至少一个假对象的集合
本文关键字:一个 集合 对象 返回 包含 | 更新日期: 2023-09-27 18:33:52
如何返回对象列表,列表中至少有一个假对象
我尝试了以下方法:
FacadeMock.Setup(x => x.GetCost(It.IsAny<IContact>()))
.Returns(() => new List<ICost>().Add(new Mock<ICost>().Object));
但它不起作用谢谢
将 lambda 的表达式替换为语句块,如以下代码所示:
FacadeMock.Setup(x => x.GetCost(It.IsAny<IContact>()))
.Returns(() => {
var myList = new List<ICost>();
myList.Add(new Mock<ICost>().Object)
// Create your real object here as much as you want
// and use myList.Add to add them to your colllection.
return myList;
}
);
想
通了:
FacadeMock.Setup(x => x.GetCost(It.IsAny<IContact>()))
.Returns(() => new List<ICost>{ new Mock<ICost>().Object });