简体   繁体   中英

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:

        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> ). Moq does not offer a generic ReturnsAsync, so how can I change the above Setup statement to work with an asynchronous method?

        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<> .

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 does offer a generic 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

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;
         }
     });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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