带有参数的Moq ReturnAsync() [英] Moq ReturnsAsync() with parameters

查看:109
本文介绍了带有参数的Moq ReturnAsync()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟这样的存储库方法

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

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

使用Moq ReturnAsync,如下所示:

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

但是我在"... ReturnAsync((WhitelistType type ...):

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

无法将lambda表达式转换为类型'Model.WhitelistItem',因为 它不是委托类型

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

WhitelistType是一个像这样的枚举:

WhitelistType is an Enum like that:

public enum WhitelistType
    {
        UserName,
        PostalCode
    }

我按小时搜索,但没有找到解决我问题的答案.

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

有任何线索吗?

推荐答案

Moq v4.5.28起

您可以将ReturnsAsync与lambda一起使用,就像在问题的代码示例中一样.无需再使用Task.FromResult().您只需需要来指定lambda委托参数的类型.否则,您将收到相同的错误消息:

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:

由于Lambda表达式不是委托类型,因此无法将其转换为"Model.WhitelistItem"类型

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

举个例子,以下适用于最新版本的Moq:

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


Moq v4.5.28之前(由 Alexei Levenkov 提供的答案)

您必须将ReturnsTask.FromResult结合使用:


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

这篇关于带有参数的Moq ReturnAsync()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆