简体   繁体   English

单元测试C#时如何moq func参数

[英]how to moq a func parameter when unit testing c#

I'm trying to write some tests but I'm not sure how to handle the arguments for setting up my moq mock. 我正在尝试编写一些测试,但是我不确定如何处理用于设置moq模拟的参数。 Can anyone suggest how I can complete this line of code correctly? 谁能建议我如何正确完成这一行代码? Many thanks, 非常感谢,

My moq code line 我的起订量代码行

在此处输入图片说明

The parameter is of type shown below for the Returns(...) which I'm not sure how to handle. 该参数的类型如下所示,我不确定该如何处理Returns(...)。

Parameter intellisense 参数智能

在此处输入图片说明

The method is shown below that I'm trying to moq out. 该方法如下所示,我正在尝试取消。

The method definition 方法定义

public static async Task<CommandResult> SendSupportAsync(this IBus bus, Command command, bool withLogging = true)

UPDATE 更新

I've tried the suggestion, I've added the mocked object and set up the return but the compiler gives the following error: 我尝试了建议,添加了模拟对象并设置了返回值,但编译器给出以下错误:

An expression tree may not contain a call or invocation that uses optional arguments

If you want to mock the return value, then you can create a mock for it 如果要模拟返回值,则可以为其创建一个模拟

var resultMock = new Mock<Task<CommandResult>>();

then setup your mock object, and you return it like 然后设置您的模拟对象,然后将其返回

bus.Setup(x=>c.SendSupportAsync(new TagCommand(ID, MID))).Returns(resultMock.object);

If you do not want to mock the result, then you can create an instance and return it like 如果您不想模拟结果,则可以创建一个实例并按如下方式返回它

bus.Setup(x=>c.SendSupportAsync(new TagCommand(ID, MID))).Returns(objCommandResult);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM