【问题标题】:How to mock in Moq Func<object, Class> Property { get; }如何模拟 Moq Func<object, Class> Property { get; }
【发布时间】:2018-03-01 11:20:09
【问题描述】:

我在使用 Moq 时遇到了模拟问题。

如何模拟这个属性?

public interface ICommand {
    Func<object, Document> GetDocument { get; }
}

【问题讨论】:

    标签: c# unit-testing moq xunit


    【解决方案1】:

    如何模拟这个属性?

    通过使用实际的函数

    给定

    public interface ICommand {
        Func<object, Document> GetDocument { get; }
    }
    

    在测试中使用一个实际的函数并从模拟接口返回它

    var mock = new Mock<ICommand>();
    
    Func<object, Document> function = (object arg) => {
        //...code to return a document
    };
    
    mock.Setup(_ => _.GetDocument).Returns(function);
    

    参考Moq Quickstart 以更好地了解如何使用模拟框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2021-05-08
      相关资源
      最近更新 更多