85

I'm trying to mock a repository's method like that

public async Task<WhitelistItem> GetByTypeValue(WhitelistType type, string value)

using Moq ReturnsAsync, like this:

static List<WhitelistItem> whitelist = new List<WhitelistItem>();

var whitelistRepositoryMock = new Mock<IWhitelistRepository>();

whitelistRepositoryMock.Setup(w => w.GetByTypeValue(It.IsAny<WhitelistType>(), It.IsAny<string>()))
                                    .ReturnsAsync((WhitelistType type, string value) =>
                                    {
                                        return (from  item in whitelist
                                                where item.Type == type && item.Value == value
                                                select item).FirstOrDefault();
                                    });

but i'm getting this error in the line "... ReturnsAsync((WhitelistType type...):

Cannot convert lambda expression to type 'Model.WhitelistItem' because it is not a delegate type

WhitelistType is an Enum like that:

public enum WhitelistType
    {
        UserName,
        PostalCode
    }

I searched by hours and didn't found any answer to my problem.

Any clues?

Daniel Medina
  • 887
  • 1
  • 6
  • 8

2 Answers2

94

From Moq v4.5.28 onwards

You can use ReturnsAsync with lambdas, exactly as in the code example of the question. No need to use Task.FromResult() any more. You just need to specify the types of the lambda delegate arguments. Otherwise you will get the same error message:

Cannot convert lambda expression to type 'Model.WhitelistItem' because it is not a delegate type

To give an example, the following works with the latest version of Moq:

whitelistRepositoryMock.Setup(w => w.GetByTypeValue(It.IsAny<WhitelistType>(), It.IsAny<string>()))
                                .ReturnsAsync((WhitelistType type, string value) =>
                                {
                                    return (from  item in whitelist
                                            where item.Type == type && item.Value == value
                                            select item).FirstOrDefault();
                                });

Before Moq v4.5.28 (answer provided by Alexei Levenkov)

You have to use Returns with Task.FromResult:

.Returns((WhitelistType type, string value) =>
 {
     return Task.FromResult(
       (from  item in whitelist
           where item.Type == type && item.Value == value
           select item).FirstOrDefault()
       );
});
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Paul Siersma
  • 2,036
  • 1
  • 22
  • 25
  • 3
    How come this works for you? I get "Cannot convert lambda expression to type x because it is not a delegate type" even after specifying the type and doing it exactly like you. Latest Moq 4.14 or something – Nick May 28 '20 at 10:00
  • I've just been doing this: `mock.Setup(o => o.MethodAsync(ArgType arg)).Returns(async arg => { var someResult = await SomeOtherAsync(arg); return someResult; });` Seems to work just fine. No need for either `Task.FromResult` or `ReturnsAsync`. – Sunday May 05 '22 at 08:56
23

I know this is an old question, but the one answer given here did not work for me and I was able to figure it out. It seems that you have to include the mocked function's argument types as type parameters to ReturnsAsync() first, followed by the mocked class type, and then the return type.

For example: .ReturnsAsync<T1, T2, TMock, TResult>((arg1, arg2) => { ... } )

Where T1, T2 are the types of your mocked function's arguments, and (arg1, arg2) are the actual arguments given when the mock was called.

So given the code from the OP, it would look like this:

whitelistRepositoryMock.Setup(w => w.GetByTypeValue(It.IsAny<WhitelistType>(), It.IsAny<string>()))
                       .ReturnsAsync<WhitelistType, string, IWhiteListRepository, WhitelistItem>((type, value) =>
                       {
                            return (from  item in whitelist
                                    where item.Type == type && item.Value == value
                                    select item).FirstOrDefault();
                       });

Edit: I realized later in the previous answer that the types are given in the lambda which does work. If you leave out the types, like I did, it will not. You would have to use the form I used.

Chad Strawinski
  • 231
  • 2
  • 4
  • Your "Edit" part saved me! Tnx – Athina Feb 08 '23 at 12:07
  • I'm getting `Cannot convert async lambda expression to delegate type 'Func'. An async lambda expression may return void, Task or Task, none of which are convertible to 'Func'. ` If I wrap last generic in ReturnsAsync with Task instead I get `ISetup>' does not contain a definition for 'ReturnsAsync' and the best extension method overload 'GeneratedReturnsExtensions.ReturnsAsync>(IReturns>>, Func>)' requires a receiver of type 'IReturns>> – koral Jul 07 '23 at 12:21