diff --git a/CHANGELOG.md b/CHANGELOG.md index 8464b93e7..deb3f995b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1 #### Fixed * AmbiguousMatchException when setting up the property, that hides another one (@ishatalkin, #939) +* `ArgumentException` ("Interface not found") when setting up `object.ToString` on an interface mock (@vslynko, #942) ## 4.13.0 (2019-08-31) diff --git a/src/Moq/ProxyFactories/InterfaceProxy.cs b/src/Moq/ProxyFactories/InterfaceProxy.cs index c1c4c278f..295dfa22a 100644 --- a/src/Moq/ProxyFactories/InterfaceProxy.cs +++ b/src/Moq/ProxyFactories/InterfaceProxy.cs @@ -27,7 +27,7 @@ public sealed override bool Equals(object obj) { // Forward this call to the interceptor, so that `object.Equals` can be set up. var interceptor = (IInterceptor)((IProxy)this).Interceptor; - var invocation = new Invocation(interceptor.GetType(), equalsMethod, obj); + var invocation = new Invocation(this.GetType(), equalsMethod, obj); interceptor.Intercept(invocation); return (bool)invocation.ReturnValue; } @@ -38,7 +38,7 @@ public sealed override int GetHashCode() { // Forward this call to the interceptor, so that `object.GetHashCode` can be set up. var interceptor = (IInterceptor)((IProxy)this).Interceptor; - var invocation = new Invocation(interceptor.GetType(), getHashCodeMethod); + var invocation = new Invocation(this.GetType(), getHashCodeMethod); interceptor.Intercept(invocation); return (int)invocation.ReturnValue; } @@ -49,7 +49,7 @@ public sealed override string ToString() { // Forward this call to the interceptor, so that `object.ToString` can be set up. var interceptor = (IInterceptor)((IProxy)this).Interceptor; - var invocation = new Invocation(interceptor.GetType(), toStringMethod); + var invocation = new Invocation(this.GetType(), toStringMethod); interceptor.Intercept(invocation); return (string)invocation.ReturnValue; } diff --git a/tests/Moq.Tests/Regressions/IssueReportsFixture.cs b/tests/Moq.Tests/Regressions/IssueReportsFixture.cs index 53cc76c24..ea4e58097 100644 --- a/tests/Moq.Tests/Regressions/IssueReportsFixture.cs +++ b/tests/Moq.Tests/Regressions/IssueReportsFixture.cs @@ -3150,6 +3150,57 @@ private void SetupIntMethod(Action callback) #endregion + #region 942 + + public class Issue942 + { + public interface IContent + { + string Name { get; set; } + } + + public interface IContainer + { + IContent Content { get; set; } + } + + private const string toStringReturnValue = "some string here"; + + [Fact] + public void Setup_ToString_before_Name() + { + this.TestImpl(contentMock => + { + contentMock.Setup(c => c.ToString()).Returns(toStringReturnValue); + contentMock.Setup(c => c.Name); + }); + } + + [Fact] + public void Setup_ToString_after_Name() + { + this.TestImpl(contentMock => + { + contentMock.Setup(c => c.Name); + contentMock.Setup(c => c.ToString()).Returns(toStringReturnValue); + }); + } + + private void TestImpl(Action> setup) + { + var contentMock = new Mock(); + var containerMock = new Mock(); + + containerMock.Setup(c => c.Content).Returns(contentMock.Object); + setup(contentMock); + + Assert.Equal(toStringReturnValue, contentMock.Object.ToString()); + Assert.Equal(toStringReturnValue, containerMock.Object.Content.ToString()); + } + } + + #endregion + // Old @ Google Code #region #47