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