尝试使用Callback
从MOQ方法获取参数值时出现问题。
// Test 1 - can get parameters in moq method callback - OK
var test = _myService.GetValues(9, 9).ToList();
// Test 2 - can not drill into moq method callback - KO
var test2 = mylist
.Select(x => _myService.GetValues(x.p1, x.p2).ToList());
// with:
var l1 = new list<int>();
var _myService = new Mock<MyService>();
_myService
.Setup(x => x.GetValues(It.IsAny<int>(), It.IsAny<int>()))
.Returns(something)
.Callback<int, int>((a, b) => l1.Add(a));
有人能解释我为什么吗?
答案 0 :(得分:3)
除非枚举Select
查询,否则不执行lambda表达式。这就是为什么它看起来不起作用。
枚举选择,一切都应按预期工作。
var test2 = mylist
.Select(x => _myService.GetValues(x.p1, x.p2).ToList())
.ToList();
OR
var test2 = mylist
.Select(x => _myService.GetValues(x.p1, x.p2).ToList());
foreach(var x in test2) { ... }