Moq Fluent接口/链方法的方法

时间:2019-05-16 10:33:33

标签: c# testing moq fluent chain

我正在使用Daniel Cazzulino的moq框架,kzu版本4.10.1。 我要先确认一下,以便我可以测试功能的特定部分(以下是我可以提取的代码的简化版本)

如此设计流利/连锁方法,以便您可以通过ID获取对象,并在需要时包括任何其他信息。

当函数调用moq'ed方法时,在获取正确的对象时遇到了一些麻烦,该方法当前返回的是错误的最后一个moq'ed对象

 /*My current Moq setup*/

class Program
{
    static void Main(string[] args)
    {
        var mock = new Mock<IFluent>();

        var c1 = new ClassA() { Id = 1, Records = new List<int>() { 5, 2, 1, 10 }, MetaData = new List<string>() };
        var c2 = new ClassA() { Id = 2, Records = new List<int>(), MetaData = new List<string>() { "X", "Y", "Z" } };

        mock.Setup(x => x.GetById(1).IncludeRecords().IncludeMetaData().Get()).Returns (c1);
        mock.Setup(x => x.GetById(2).IncludeRecords().IncludeMetaData().Get()).Returns(c2);

        var result = new ComputeClass().ComputeStuff(mock.Object);
        Console.WriteLine(result);
        Console.ReadLine();
    }
}

/*Fluent interface and object returned*/
public interface IFluent
{
    IFluent GetById(int id);
    IFluent IncludeRecords();
    IFluent IncludeMetaData();
    ClassA Get();
}
public class ClassA
{
    public int Id { get; set; }
    public ICollection<int> Records { get; set; }
    public ICollection<string> MetaData { get; set; }
}

 /*the method which is doing the work*/
public class ComputeClass
{
    public string ComputeStuff(IFluent fluent)
    {
        var ids = new List<int>() { 1, 2 };
        var result = new StringBuilder();
        foreach (var id in ids)
        {
            var resClass = fluent.GetById(id).IncludeRecords().IncludeMetaData().Get();
            result.Append($"Id : {id}, Records: {resClass.Records.Count}, MetaData: {resClass.MetaData.Count}{Environment.NewLine}");
        }
        return result.ToString();
    }
}

   Current incorrect result 
    /*Id : 1, Records: 0, MetaData: 3
      Id : 2, Records: 0, MetaData: 3*/

     Expected Result
    /*Id : 1, Records: 3, MetaData: 0
      Id : 2, Records: 0, MetaData: 3*/

2 个答案:

答案 0 :(得分:3)

最简单的方法是拆分每个设置:

var mock = new Mock<IFluent>();
var mock1 = new Mock<IFluent>();
var mock2 = new Mock<IFluent>();

mock.Setup(x => x.GetById(1)).Returns(mock1.Object); 
mock1.Setup(x => x.IncludeRecords()).Returns(mock1.Object);
mock1.Setup(x => x.IncludeMetaData()).Returns(mock1.Object);
mock1.Setup(x => x.Get()).Returns(c1);

mock.Setup(x => x.GetById(2)).Returns(mock2.Object); 
mock2.Setup(x => x.IncludeRecords()).Returns(mock2.Object);
mock2.Setup(x => x.IncludeMetaData()).Returns(mock2.Object);
mock2.Setup(x => x.Get()).Returns(c2);

var result = new ComputeClass().ComputeStuff(mock.Object);

如果您想要一些更复杂的东西,可以创建一个扩展/实用程序来为您处理所有这些,请查看此博客文章:https://www.codemunki.es/2014/11/20/mocking-a-fluent-interface-automatically-in-moq/

答案 1 :(得分:1)

仅是对现有答案的补充。对于模拟fluent API,在 moq 中有一个有用的选项,即SetReturnsDefault,它可以节省一些模拟,特别是在您拥有大量fluent API的情况下,例如

var mock = new Mock<IFluent>();
var mock1 = new Mock<IFluent>();
var mock2 = new Mock<IFluent>();

mock.Setup(x => x.GetById(1)).Returns(mock1.Object);
mock1.SetReturnsDefault(mock1.Object);
mock1.Setup(x => x.Get()).Returns(a);

mock.Setup(x => x.GetById(2)).Returns(mock2.Object);
mock2.SetReturnsDefault(mock2.Object);
mock2.Setup(x => x.Get()).Returns(b);

var aa = mock.Object.IncludeMetaData().GetById(1).IncludeMetaData().Get();
var bb = mock.Object.IncludeMetaData.GetById(2).IncludeMetaData.Get();

使用这种方法,您实际上只需要模拟与Fluent API不同的方法,而不必模拟所有方法。