I want to moq a property that has an index, and I want to be able to use the index values in the callback, the same way you can use method arguments in the callback for moq\
As of Moq 4.2.1502.0911 for .NET Framework 4.5, I found the following behavior to be true.
The problem you're running into is specifically due to the It.IsAny<T>
call. If you use It.IsAny<T>
, the callback does not execute:
[Test]
public void DoesNotExecuteCallback()
{
// Arrange
var mock = new Mock<IIndexable>();
var called = false;
mock.SetupSet(m => m[It.IsAny<int>()] = It.IsAny<int>()).Callback<int,int>((x, y) => called = true);
var instance = mock.Object;
// Act
instance[1] = 2;
// Arrange
Assert.That(called, Is.False);
}
But if you call it using specific parameters, it calls the callback:
[Test]
public void DoesExecuteCallback()
{
// Arrange
var mock = new Mock<IIndexable>();
var called = false;
mock.SetupSet(m => m[1] = 2).Callback<int,int>((x, y) => called = true);
var instance = mock.Object;
// Act
instance[1] = 2;
// Arrange
Assert.That(called, Is.True);
}
To summarize, you should avoid the use of the It.IsAny
when trying to setup expectations for an indexer. In general, you should be avoiding the use of it because it can encourage sloppy test writing
There are appropriate use cases for It.IsAny
, but without knowing the specifics I tend to recommend against it.
The method SetupSet
takes a plain delegate, not an expression tree of type Expression<...> like many other Moq methods.
For that reason Moq cannot see that you used It.IsAny
. Instead, It.IsAny
is called (not what we want) and Moq sees its return value only which happens to be default(int)
, or 0
.