简体   繁体   English

起订量:更改设置返回处理异步参数的通用方法

[英]Moq: Change Setup Returns Generic method to deal with Asynchronous Parameter

I have a Moq setup statement that looks like this with a conditional/generic Returns statement based on the enum value passed in:我有一个起订量设置语句,看起来像这样,带有一个基于传入枚举值的条件/通用 Returns 语句:

        MyLogic.Setup(m => m.MyLogicMethod(It.IsAny<MyEnum>()))
                                     .Returns<MyEnum>((x) =>
                                     {
                                         switch (x)
                                         {
                                             case MyEnum.Enum1: return "stringval1";
                                             case MyEnum.Enum2: return "stringval2";
                                             case MyEnum.Enum3: return "stringval3";
                                             case MyEnum.Enum4: return "stringval4";
                                             case MyEnum.Enum5: return "stringval5";
                                             default: return string.Empty;
                                         }
                                     });

I have now changed the method being tested to be async (returning Task<string> ).我现在已将正在测试的方法更改为异步(返回Task<string> )。 Moq does not offer a generic ReturnsAsync, so how can I change the above Setup statement to work with an asynchronous method? Moq 不提供通用 ReturnsAsync,那么如何更改上述 Setup 语句以使用异步方法?

        MyLogic.Setup(m => m.MyLogicMethodAsync(It.IsAny<MyEnum>()))
                                     .Returns????

You don't have to use ReturnAsync , you can keep your return method, just return a Task<> .您不必使用ReturnAsync ,您可以保留您的返回方法,只需返回一个Task<>

MyLogic.Setup(m => m.MyLogicMethod(It.IsAny<MyEnum>()))
                                 .Returns<MyEnum>((x) =>
                                 {
                                     string returnValue = null;
                                     switch (x)
                                     {
                                         case MyEnum.Enum1: returnValue = "stringval1";
                                         case MyEnum.Enum2: returnValue = "stringval2";
                                         case MyEnum.Enum3: returnValue = "stringval3";
                                         case MyEnum.Enum4: returnValue = "stringval4";
                                         case MyEnum.Enum5: returnValue = "stringval5";
                                         default: returnValue = string.Empty;
                                     }

                                     return Task.FromResult(returnValue);
                                 });

Moq does not offer a generic ReturnsAsync, Moq 不提供通用 ReturnsAsync,

Moq does offer a generic ReturnsAsync . Moq 确实提供了一个通用的ReturnsAsync

//
// Summary:
//     Specifies a function that will calculate the value to return from the asynchronous
//     method.
//
// Parameters:
//   mock:
//     Returns verb which represents the mocked type and the task of return type
//
//   valueFunction:
//     The function that will calculate the return value.
//
// Type parameters:
//   TMock:
//     Mocked type.
//
//   TResult:
//     Type of the return value.
public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, Func<TResult> valueFunction) where TMock : class;

Assembly Moq, Version = 4.15.0.0最小起订量,版本 = 4.15.0.0

Move your generic argument into the delegate so that the it can be implied by the generic method.将您的泛型参数移动到委托中,以便泛型方法可以隐含它。

MyLogic
    .Setup(m => m.MyLogicMethodAsync(It.IsAny<MyEnum>()))
    .ReturnsAsync((MyEnum x) => {
         switch (x) {
             case MyEnum.Enum1: return "stringval1";
             case MyEnum.Enum2: return "stringval2";
             case MyEnum.Enum3: return "stringval3";
             case MyEnum.Enum4: return "stringval4";
             case MyEnum.Enum5: return "stringval5";
             default: return string.Empty;
         }
     });

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

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