devlooped / devlooped/moq

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

Open
#1,166 3 comments 0 reactions 1 assignee View on GitHub

@tonyhallett is already working on this.

Since Jul 20, 2021.

enhancement stale
Dominant language
C#
Stars
6.4k
Forks
835
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.