Reputation: 761
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????
Upvotes: 1
Views: 1072
Reputation: 247223
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;
}
});
Upvotes: 1
Reputation: 25370
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);
});
Upvotes: 2