Moq返回一个集合,其中至少有一个假对象

时间:2016-02-09 22:06:40

标签: c# moq

如何返回一个对象列表,列表中至少有一个假对象

我尝试了以下内容:

    FacadeMock.Setup(x => x.GetCost(It.IsAny<IContact>()))
        .Returns(() => new List<ICost>().Add(new Mock<ICost>().Object));

但它不起作用 感谢

2 个答案:

答案 0 :(得分:1)

将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;
    }
);

答案 1 :(得分:0)

想出来:

        FacadeMock.Setup(x => x.GetCost(It.IsAny<IContact>()))
            .Returns(() => new List<ICost>{ new Mock<ICost>().Object });