MoqProtectedLike 2.0.1

A fully functioning Moq protected solution.

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.

[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);
}


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);
}


  • .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 History

Version Downloads Last updated
2.0.1 54 5/28/2021
2.0.0 63 5/28/2021
1.0.0 12,254 11/27/2018