MoqProtectedLike 2.0.1

.NET Standard 2.0 .NET Framework 4.5
Install-Package MoqProtectedLike -Version 2.0.1
dotnet add package MoqProtectedLike --version 2.0.1
<PackageReference Include="MoqProtectedLike" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MoqProtectedLike --version 2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MoqProtectedLike, 2.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MoqProtectedLike as a Cake Addin
#addin nuget:?package=MoqProtectedLike&version=2.0.1

// Install MoqProtectedLike as a Cake Tool
#tool nuget:?package=MoqProtectedLike&version=2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

MoqProtectedLike - Why ?

ProtectedMock does not work properly.

SetUpSet ignores the object value and uses ItExpr.IsAny<TProperty>() This is contrary to the xml docs. You are forced to supply TProperty and if you want to set up an indexer you have to cast.

VerifySet also ignores the object value. The behaviour of ItExpr.IsAny<TProperty>() can be seen with this ridiculous test.

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

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

Indexers are not properly supported.

You can workaround these issues by using Verify and Setup. For instance

public void SetUp_Workaround_For_SetupSet_Object_Ignored()
	var mock = new Mock<Foo>();
	var protectedMock = mock.Protected();
	protectedMock.Setup("set_ReadWritePropertyImpl", 999);

public void SetUp_Workaround_For_SetupGet_Overloaded_Indexers()
	var mock = new Mock<OverloadedIndexers>();
	var protectedMock = mock.Protected();
	//ambiguous match exception if provide Item
	protectedMock.Setup<int>("get_Item", new object[] { ItExpr.IsInRange(3, 4, Moq.Range.Inclusive) }).Returns(123);
	protectedMock.Setup<int>("get_Item", new object[] { ItExpr.Is<string>(index => index.StartsWith("Match")) }).Returns(456);

ProtectedAsMock also has issues

Recursive mocking does not work.

Virtual properties with protected accessors does not work.

There is no SetupSet or VerifySet.



Corrects the issues with ProtectedAsMock and provides SetupSet and VerifySet.


Use in a similar manner to ProtectedAsMock

public void Should_Work_With_Recursive_Mocks()
	//does not work in Moq
	var mock = new Mock<Foo>();
	var protectedAs = mock.Protected().As<Fooish>();
	protectedAs.SetupGet(f => f.Nested.Deep.Value).Returns(123);
	Assert.Throws<ArgumentException>(() => Assert.AreEqual(123, mock.Object.GetNested().Deep.Value));

	//does here
	var protectedMock = new Mock<Foo>().IsProtected().Like<Fooish>();
	protectedMock.SetupGet(f => f.Nested.Deep.Value).Returns(123);
	//note that you can get the proxy with Object
	Assert.AreEqual(123, protectedMock.Object.GetNested().Deep.Value);

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • Moq (>= 4.16.1)
  • .NETStandard 2.0

    • Moq (>= 4.16.1)
  • .NETStandard 2.1

    • Moq (>= 4.16.1)

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 2,060 5/28/2021
2.0.0 167 5/28/2021
1.0.0 17,432 11/27/2018