devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.93k stars 801 forks source link

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

Open tonyhallett opened 3 years ago

tonyhallett commented 3 years ago

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

stakx commented 3 years ago

@tonyhallett, would you be OK with...

Then we can more precisely associate PRs with existing issues, and have them auto-closed separately via a "fixes #..." comments.

tonyhallett commented 3 years ago

@stakx No problem.

The draft pull request has the missing indexer support

github-actions[bot] commented 2 months ago

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.