我有一个声明的界面
Task DoSomethingAsync();
Run Code Online (Sandbox Code Playgroud)
我正在使用MoqFramework进行测试:
[TestMethod()]
public async Task MyAsyncTest()
{
Mock<ISomeInterface> mock = new Mock<ISomeInterface>();
mock.Setup(arg => arg.DoSomethingAsync()).Callback(() => { <my code here> });
...
}
Run Code Online (Sandbox Code Playgroud)
然后在我的测试中我执行调用的代码await DoSomethingAsync().而且测试在这条线上失败了.我究竟做错了什么?
我正在尝试模拟存储库的方法
public async Task<WhitelistItem> GetByTypeValue(WhitelistType type, string value)
Run Code Online (Sandbox Code Playgroud)
使用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();
});
Run Code Online (Sandbox Code Playgroud)
但我在行中收到此错误"... ReturnsAsync((WhitelistType type ...):
无法将lambda表达式转换为类型'Model.WhitelistItem',因为它不是委托类型
WhitelistType是这样的枚举:
public enum WhitelistType
{
UserName,
PostalCode
}
Run Code Online (Sandbox Code Playgroud)
我按小时搜索,没有找到任何问题的答案.
有线索吗?