我试图模拟一个仓库的方法
public async Task<WhitelistItem> GetByTypeValue(WhitelistType type,string value)
使用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(); });
但我得到这个错误在行“… ReturnsAsync((白名单类型…):
Cannot convert lambda expression to type ‘Model.WhitelistItem’ because
it is not a delegate type
WhitelistType是一个枚举:
public enum WhitelistType { UserName,PostalCode }
我几个小时搜查,没有找到任何答案我的问题.
任何线索?
解决方法
ReturnsAsync不具有返回值的lambda版本.
修复:使用返回与Task.FromResult`
.Returns((WhitelistType type,string value) => { return Task.FromResult( (from item in whitelist where item.Type == type && item.Value == value select item).FirstOrDefault() ); });