From 8f69c60fd548385397647d37ceb0f7aeb70dfb9a Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Thu, 12 May 2022 17:54:37 +0200 Subject: [PATCH 1/3] Add regression test for `SetupProperty` --- .../Regressions/IssueReportsFixture.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/Moq.Tests/Regressions/IssueReportsFixture.cs b/tests/Moq.Tests/Regressions/IssueReportsFixture.cs index c4dc04dd2..798ca0c48 100644 --- a/tests/Moq.Tests/Regressions/IssueReportsFixture.cs +++ b/tests/Moq.Tests/Regressions/IssueReportsFixture.cs @@ -3781,6 +3781,42 @@ public void Property_on_submock_should_be_stubbed_2() #endregion + #region 1248 + + public class Issue1248 + { + public interface IBase + { + bool Property { get; set; } + } + + public interface IDerived : IBase + { + } + + public class Base : IBase + { + public virtual bool Property { get; set; } + } + + [Fact] + public void Test() + { + var mock = new Mock(); + var mockAsDerived = mock.As(); + mockAsDerived.SetupProperty(x => x.Property, false); + + mockAsDerived.Object.Property = true; + + mock.VerifySet(x => x.Property = true, Times.Once()); + mockAsDerived.VerifySet(x => x.Property = true, Times.Once()); + Assert.True(mockAsDerived.Object.Property); + Assert.True(mock.Object.Property); + } + } + + #endregion + #region 1249 public class Issue1249 From 2c14374046bbb3a546c31a04b481696812f3076e Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Thu, 12 May 2022 17:55:02 +0200 Subject: [PATCH 2/3] `StubbedPropertySetup.IsMatch` is too picky Comparing `MethodInfo`s using the `==` operator appears to go wrong more often than not. (In this case, this method fails to identify a positive match of `Base.Property` as the implementation for `IBase.Property`). Let's make this test much more imprecise by simply comparing accessor method names (and thus completely ignoring the type hierarchy; if we're lucky, the .NET compilers will already guarantee valid polymorphic type relationships). --- src/Moq/StubbedPropertySetup.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Moq/StubbedPropertySetup.cs b/src/Moq/StubbedPropertySetup.cs index 17bcd5df6..afe2e0ae8 100644 --- a/src/Moq/StubbedPropertySetup.cs +++ b/src/Moq/StubbedPropertySetup.cs @@ -100,8 +100,8 @@ public override int GetHashCode() public override bool IsMatch(Invocation invocation) { - var method = invocation.Method; - return method == this.getter || method == this.setter; + var methodName = invocation.Method.Name; + return methodName == this.getter.Name || methodName == this.setter.Name; } } } From 7a7a76b0a8245b67683873402ec78fe591a0dc75 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Thu, 12 May 2022 18:02:48 +0200 Subject: [PATCH 3/3] Update the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9be8f1824..d3726adf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ New major version of DynamicProxy (you may get better performance!), so please u * Can't set up "private protected" properties (@RobSiklos, #1170) * Using [...] an old version of `System.Net.Http` which is vulnerable to "DoS", "Spoofing", "Privilege Escalation", "Authentication Bypass" and "Information Exposure" (@sidseter, #1219) +* Regression with `SetupProperty` where Moq fails to match a property accessor implementation against its definition in an interface (@Naxemar, #1248) * Failure when invoking a method with by-ref parameter & mockable return type on a mock with `CallBase` and `DefaultValue.Mock` configured (@IanKemp, #1249)