#1166·moq

`mock.Protected()` does not properly support indexers

Author: tonyhallettCreated May 29, 2021Updated Sep 2, 2024
Labelsenhancementstale

MoqProtected can be improved. MoqProtectedAs does not suffer these issues. I think that despite there being an alternative this should be addressed.

SetUpSet ignores the value parameter and uses ItExpr.IsAny() This is contrary to the xml docs.

VerifySet also ignores the value parameter. This behaviour is best shown with Moq's own tests

https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/tests/Moq.Tests/ProtectedMockFixture.cs#L806

		[Fact]
		public void VerifySetAllowsProtectedInternalPropertySet()
		{
			var mock = new Mock<FooBase>();
			mock.Object.ProtectedInternalValue = "foo";

			mock.Protected().VerifySet<string>("ProtectedInternalValue", Times.Once(), "bar");
		}

and https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/tests/Moq.Tests/ProtectedMockFixture.cs#L834

		[Fact]
		public void DoesNotThrowIfVerifySetPropertyTimesReached()
		{
			var mock = new Mock<FooBase>();
			mock.Object.SetProtectedValue("foo");
			mock.Object.SetProtectedValue("foo");

			mock.Protected().VerifySet<string>("ProtectedValue", Times.Exactly(2), ItExpr.IsAny<int>());
		}

Indexers are not properly supported.

These are not so important https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/src/Moq/Protected/ProtectedMock.cs#L92 https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/src/Moq/Protected/ProtectedMock.cs#L186 https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/src/Moq/Protected/ProtectedMock.cs#L267

This ( and SetupSet ) should be supported.

should receive args to support indexers

It already does.

https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/src/Moq/Protected/ProtectedMock.cs#L293

SetupGet and VerifyGet should also allow for indexers.

Back this issue Back this issue