The following tests pass with Moq 4.11.0 and fail with 4.12.0:
var httpContext = Mock.Of<Microsoft.AspNetCore.Http.HttpContext>();
httpContext.Items = new Dictionary<object, object>();
Assert.NotNull(httpContext.Items);
var httpContext = new Mock<Microsoft.AspNetCore.Http.HttpContext>()
.SetupAllProperties();
httpContext.Object.Items = new Dictionary<object, object>();
Assert.NotNull(httpContext.Object.Items);
This seems to be very likely caused by https://github.com/moq/moq4/pull/826
Perhaps this new code doesn't handle abstract types correctly?
Environment: Windows x64, .NET Core 2.2.1
This pattern also fails:
var httpContext = Mock.Of<HttpContext>(
context => context.Items == new Dictionary<object, object>());
Assert.NotNull(httpContext.Items);
Thanks for reporting, @mattzink. I've found the regression. The problem lies here:
https://github.com/moq/moq4/blob/731567c12159d855658726479eda8d25626ae8b9/src/Moq/Extensions.cs#L51-L59
.StartsWith should be .Equals. I'm about to push a bug fix to master.
Ahh, so it's because the property name is "Items" that it thinks it's an indexer. Interesting!
Yes, SetupAllProperties does not set up indexers (and never has, AFAIK). Under the hood, indexers look like parameterized properties called Item. (Though the name can be customized, which Moq currently doesn't account for.)
Fixed in master. Watch out for an updated version of Moq later this month!
So even with this fix, this issue remains for properties called Item, then?
@kaan-kaya, good point. I suppose it's time to finally fix the indexer recognition logic for good.