简体   繁体   English

带参数的 Moq ReturnsAsync()

[英]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:使用 Moq ReturnsAsync,如下所示:

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...):但我在“... ReturnsAsync((WhitelistType type...):

Cannot convert lambda expression to type 'Model.WhitelistItem' because it is not a delegate type无法将 lambda 表达式转换为类型“Model.WhitelistItem”,因为它不是委托类型

WhitelistType is an Enum like that: WhitelistType 是一个像这样的枚举:

public enum WhitelistType
    {
        UserName,
        PostalCode
    }

I searched by hours and didn't found any answer to my problem.我搜索了几个小时,但没有找到我的问题的任何答案。

Any clues?有什么线索吗?

From Moq v4.5.28 onwardsMoq v4.5.28 开始

You can use ReturnsAsync with lambdas, exactly as in the code example of the question.您可以将ReturnsAsync与 lambdas 一起使用, ReturnsAsync与问题的代码示例完全相同。 No need to use Task.FromResult() any more.不再需要使用Task.FromResult()了。 You just need to specify the types of the lambda delegate arguments.您只需要指定 lambda 委托参数的类型。 Otherwise you will get the same error message:否则,您将收到相同的错误消息:

Cannot convert lambda expression to type 'Model.WhitelistItem' because it is not a delegate type无法将 lambda 表达式转换为类型“Model.WhitelistItem”,因为它不是委托类型

To give an example, the following works with the latest version of Moq:举个例子,以下适用于最新版本的 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 )在 Moq v4.5.28 之前( Alexei Levenkov提供的答案)

You have to use Returns with Task.FromResult :您必须将ReturnsTask.FromResult一起使用:

.Returns((WhitelistType type, string value) =>
 {
     return Task.FromResult(
       (from  item in whitelist
           where item.Type == type && item.Value == value
           select item).FirstOrDefault()
       );
});

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.看来,你必须包括嘲笑函数的参数类型的类型参数ReturnsAsync()第一,其次是嘲笑类类型,然后返回类型。

For example: .ReturnsAsync<T1, T2, TMock, TResult>((arg1, arg2) => { ... } )例如: .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.其中T1, T2是模拟函数参数的类型, (arg1, arg2)是调用模拟时给出的实际参数。

So given the code from the OP, it would look like this:因此,鉴于 OP 中的代码,它看起来像这样:

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.编辑:我后来在上一个答案中意识到类型是在确实有效的 lambda 中给出的。 If you leave out the types, like I did, it will not.如果像我一样省略类型,则不会。 You would have to use the form I used.你必须使用我使用的表格。

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

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