Moq Fluent接口/链方法的方法 [英] how to Moq Fluent interface / chain methods

查看:89
本文介绍了Moq Fluent接口/链方法的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the moq framework by Daniel Cazzulino, kzu Version 4.10.1. I want to moq so i can test a particular part of functionality (below is the simplistic version of the Code i could extract)

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

The fluent/chain method so are designed so you can get object by an Id and include any additional information if required.

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

i'm having some trouble fetching the correct object when the function is calling the moq'ed method, which is currently returning the last moq'ed object which is wrong

 /*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*/

推荐答案

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

The easiest way would be to split out each setup:

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-自动订量/

You could create an extension/utility to handle this all for you if you wanted something a bit more complex, take a look at this blog post: https://www.codemunki.es/2014/11/20/mocking-a-fluent-interface-automatically-in-moq/

这篇关于Moq Fluent接口/链方法的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆