Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

c# - Moq ReturnsAsync() with parameters

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...