MoqProtectedLike 2.0.1

dotnet add package MoqProtectedLike --version 2.0.1                
NuGet\Install-Package MoqProtectedLike -Version 2.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: MoqProtectedLike, 2.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. 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                

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.

[Fact]
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


[Fact]
public void SetUp_Workaround_For_SetupSet_Object_Ignored()
{
	var mock = new Mock<Foo>();
	var protectedMock = mock.Protected();
	protectedMock.Setup("set_ReadWritePropertyImpl", 999);
	//...
}


[Fact]
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.

 

MoqProtectedLike

Corrects the issues with ProtectedAsMock and provides SetupSet and VerifySet.

Usage

Use in a similar manner to ProtectedAsMock

[Test]
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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 18,279 5/28/2021
2.0.0 367 5/28/2021
1.0.0 19,336 11/27/2018