I have a problem with verifying that certain method was called on mock inside LINQ Select() query.
Here is the method of ContentManager I want to test:
public string ProcessElements(List<Item> items)
{
var processed = items.Select(item => item.Process(Constants.Text));
return Serialize(processed);
}
I want to test that Process() is called for elements of the list. My test method looks like this:
[TestMethod]
public void ProcessItems_ValidItems_ProcessCalled()
{
var contentManager = new ContentManager();
var itemMock = new Mock<Item>();
itemMock.Setup(m => m.Process(It.IsAny<string>()))
.Returns("serialized");
contentManager.ProcessElements(new List<Item>() { itemMock.Object });
itemMock.Verify(m => m.Process(It.IsAny<string>()), Times.Once());
}
When I run this test it fails and returns the following message:
Test method ProcessItems_ValidItems_ProcessCalled threw exception:
Moq.MockException:
Expected invocation on the mock at least once, but was never performed:
m => m.Process(It.IsAny())
No setups configured.
No invocations performed.
But if I change Select() to foreach, then test passes successfully:
public string ProcessElements(List<Item> iitem)
{
var processed = new List<string>();
foreach (var item in iitem)
{
processed.Add(item.Process(Constants.Text));
}
return Serialize(processed);
}
What is wrong with Moq + Select()? How can I fix this?