如何在 Moq 中模拟 Func<object, Class> 属性 { get; }

How to mock in Moq Func<object, Class> Property { get; }

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

如何模拟这个 属性?

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

How do I mock this property?

通过使用实际函数

给出

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

在测试中使用实际函数,return 从模拟接口

var mock = new Mock<ICommand>();

Func<object, Document> function = (object arg) => {
    //...code to return a document
};

mock.Setup(_ => _.GetDocument).Returns(function);

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