c# - 如何在 Moq Func<object, Class> Property { get; 中进行模拟}

标签 c# unit-testing moq xunit

我陷入了使用 Moq 的模拟问题。

我如何模拟这个属性?

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

最佳答案

How do I mock this property?

通过使用实际函数

给定

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

在测试中使用实际函数并从模拟接口(interface)返回该函数

var mock = new Mock<ICommand>();

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

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

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

关于c# - 如何在 Moq Func<object, Class> Property { get; 中进行模拟},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49048588/

相关文章:

C#通过属性获取字段值(PropertyInfo)

java - 在非常简单的示例中使用 EasyMock.expect() 时编译错误?

c# - 动态绑定(bind)到资源的 "Path"

c# - 连接到 sql server 数据库 mdf 文件而不在客户端机器上安装 sql server?

c# - 使用 AutoMoq 属性模拟构造函数依赖

c# - 如何对 Web API 中冲突响应的错误消息进行单元测试

c# - 尝试为 ASP.Net Core 3.1 单元测试创​​建 Mock.Of<ControllerContext>() 时出错

c# - 将 AutoFixture 与最小起订量一起使用?

c# - 存储库中的 Moq 函数,以 lambda 表达式作为参数

C# ListView 图像在运行时不显示