MoqProtectedLike 2.0.1
Install-Package MoqProtectedLike -Version 2.0.1
dotnet add package MoqProtectedLike --version 2.0.1
<PackageReference Include="MoqProtectedLike" Version="2.0.1" />
paket add MoqProtectedLike --version 2.0.1
#r "nuget: MoqProtectedLike, 2.0.1"
// 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 | 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 |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.