From 1dc115dafcedad27f3abb4159ed00729dd9833a8 Mon Sep 17 00:00:00 2001 From: stakx Date: Thu, 28 Sep 2017 21:31:59 +0200 Subject: [PATCH 1/3] Add failing unit tests This one shows that if a mock is "cast" to an interface before its object is queried, and the proxy is then generated via the `.As` mock, then an proxy object is created that is different from the proxy object retrieved from the original, uncast mock. --- UnitTests/AsInterfaceFixture.cs | 2 ++ UnitTests/Regressions/IssueReportsFixture.cs | 22 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/UnitTests/AsInterfaceFixture.cs b/UnitTests/AsInterfaceFixture.cs index c1f444d0c..bb42e293e 100644 --- a/UnitTests/AsInterfaceFixture.cs +++ b/UnitTests/AsInterfaceFixture.cs @@ -169,6 +169,8 @@ public void ShouldNotThrowIfCallExplicitlyImplementedInterfacesMethodWhenCallBas bag.Get("test"); } + // see also test fixture `Issue458` in `IssueReportsFixture` + public interface IFoo { void Execute(); diff --git a/UnitTests/Regressions/IssueReportsFixture.cs b/UnitTests/Regressions/IssueReportsFixture.cs index 33b85f9e7..e79e1a69b 100644 --- a/UnitTests/Regressions/IssueReportsFixture.cs +++ b/UnitTests/Regressions/IssueReportsFixture.cs @@ -1577,6 +1577,28 @@ public class Foo #endregion + #region 458 + + public class Issue458 + { + [Fact] + public void Mock_Object_always_returns_same_object_even_when_first_instantiated_through_AsInterface_cast() + { + Mock mock = new Mock().As(); + + object o1 = ((Mock)mock).Object; + object o2 = mock.Object; + + Assert.Same(o1, o2); + } + + public interface IFoo { } + + public class Foo : IFoo { } + } + + #endregion + // Old @ Google Code #region #47 From 257148613a5d452a85740e6304498c5931e296f7 Mon Sep 17 00:00:00 2001 From: stakx Date: Thu, 28 Sep 2017 21:34:07 +0200 Subject: [PATCH 2/3] Let `.As` mocks generate same proxy as the uncast mock --- Source/AsInterface.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/AsInterface.cs b/Source/AsInterface.cs index 4230bb610..79abe9c0c 100644 --- a/Source/AsInterface.cs +++ b/Source/AsInterface.cs @@ -99,5 +99,10 @@ public override Mock As() { return this.owner.As(); } + + protected override object OnGetObject() + { + return this.owner.Object; + } } } From 432e8b973e06cdb1155bf2d984e851be3b9c35a5 Mon Sep 17 00:00:00 2001 From: stakx Date: Thu, 28 Sep 2017 21:47:31 +0200 Subject: [PATCH 3/3] Update the changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be303723c..b4e55375e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1 * Update package reference to `Castle.Core` (DynamicProxy) from version 4.1.1 to 4.2.0, which uses a new assembly versioning scheme that should eventually reduce assembly version conflicts and the need for assembly binding redirects (@stakx, #459) +#### Fixed + +* `mock.Object` should always return the exact same proxy object, regardless of whether the mock has been cast to an interface via `.As()` or not (@stakx, #460) + ## 4.7.127 (2017-09-26)