2

I have an issue while trying to get parameter values from MOQ Method using Callback.

// 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));

Can someone explain me why ?

2
  • what errors are u getting? what are something and mylist? Commented Nov 15, 2016 at 17:18
  • Provide a minimal reproducible example that reproduces the problem other wise the question will remain unclear and will be closed. Commented Nov 15, 2016 at 17:26

1 Answer 1

3

Unless the Select query is enumerated the lambda expression is not executed. That is why it appears not to work.

Enumerate the select and everything should work as expected.

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) { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.